![]() |
![]() |
|
home—info—lectures—exams—hws—archive
See: Java syntax for 120 (which is incomplete and technically wrong in some places), and BNF for java Example: Suppose we want to define standard Algol-like "expressions", but where we only involve numbers, +, * and parens: 3+5 (3+5)*4 3+5*4 3+(4*5)+((((7)))) (We'll explore ambiguity later (incl. a way to modify the grammar to enforce precedence).) Note: issues of *syntax* vs *semantics*; what about a grammar for martian-math, which has operators `@` and `$`, which might mean "take average" and "take min" [or anything else; the grammar doesn't care]? What is grammar for scheme's 'cond'? A grammar for all scheme: - literal values number, boolean, string, symbol, image - variable - define [for constant] define [for functions] - func-call - "special forms": cond, if, and, or. Well okay, also: - define-struct Btw, reflect on the BNF rules we used to allow a list of arguments. Here's a data definition: ; A list is: ; - empty ; - (cons [number] [list]) ; Examples of the data: ; (Btw: is `list` a union type, or an aggregate type?) ; A function to handle a list. ; What does the design recipe give us, as a template? ; We'll write: ; sum : list-of-number -> ; length : list-of-number -> ; square-each : list-of-number -> ; ; total-book-time : list-of-book -> ; vandalize* : list-of-book ->
(To be discussed in further future:)
We saw last time, BNF for specifying grammars; derivations in a grammar; the parse-tree associated with a derivation; grammars can be ambiguous; you can encode precedence-of-operations via grammar productions.
What is a BNF for XML? Let's use very basic XML -- no attributes, no entities, and no initial format/namespace tags.
As noted, the BNF for XML is the data definition. What would any code processing XML look like?
Okay, now let's look at parsing: given a flat string of chars, turn it into an instance of our data def'n. (In other words: create a parse tree.)
For each nonterminal X, we'll have a method readX : String → myXstruct. (Actually, in Java, we'll use a Scanner instead of a String.)
This is called recursive descent parsing (§4.4).
Note that XML is a particularly simple language: not only is it non-ambiguous, but we can peek at just the first few characters of the string to tell whether we are reading a string, an open-tag, or a close-tag. (As opposed to an AExpr, where "23456 + 11" you need to look quite a few characters ahead to tell if this is a 'high-priority-term' or 'low-priority-term'.)
Compare XML to Sexpr.
home—info—lectures—exams—hws—archive
©2009, Ian Barland, Radford University Last modified 2009.Sep.23 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |
![]() |