Definition Language definition lexical rules syntax rules EBNF (for defining syntax) Regular expressions Syntax and learning programming languages (ex: Scheme function calls)
Definition
Syntax defines the allowable sequences of words in a language Syntax does affect the "power" of the language ex: language with only pronouns and nouns "Me Tarzan, you Jane"
Goals (continued)
ease of translation -- ex: Lisp see example in EBNF discussion below avoid ambiguity if ... if ... else ... //match "else" to which "if"? syntax must answer this question
Syntactic rules: how the words can be combined ex: (a = b) is OK, but (a = b = c) is not
Grammatical definition of a sentence of English? Define each grammatical construct by possible sequences of components Ex: Possibilities for the subject (noun part) of a sentence?
EBNF
notation for defining grammar of a language each grammar includes: terminals -- symbols/words in the language nonterminals -- a unit representing a grammatically correct sequence of
terminals usually one nonterminal is considered the "start" symbol productions -- the valid ways a nonterminal could be replaced by terminals and other nonterminals
EBNF (continued)
ex: English sentence -> noun-part verb-part . noun-part -> noun | adj noun -> may be replaced by ::= sentence would be start symbol for empty string $$ for end of input in production -- can help identify start symbol
EBNF (continued)
nonterminals in italics (or surrounded by >) | means "or" * to indicate 0 or more of something, + 1 or more: ex: posInteger -> digit+ digit -> 0 | 1 | 2 | ... | 9
Lisp in EBNF
expression -> atom | list atom -> number | name | string | operator list -> (expression*) close to complete -- compare with C++
EBNF in EBNF
EBNF-grammar -> production* production -> nonterminal -> list list -> expr | expr | list expr -> term | term expr term -> item | item* | item+ item -> nonterminal | terminal | (expr)
Examples
372 for posInteger (+ 2 5) for Lisp grammar (exit) for Lisp grammar Fig's. 2.1-2.4 in Scott
Regular expressions
regular expression = terminal symbol if a and b are reg. expr.'s, so are: a | b, ab, (a), a* nothing else
More Scheme
(cons (quote a) (quote (b c))) ; cons constructs list 1st parameter is 1st element of list 2nd parameter is rest of list (cons '(a b) '(c)) ; compare with previous line
More Scheme
Some erroneous lines: (f) ; Scheme cannot find definition for f (cons a (b c)) ; similar (cons 'a '(b) ; Scheme waits for you (to do what?) Where are missing semi-colons detected in C++/Java compilers?
Interpreter modules
Read need complete expression Evaluate
Print every expression must have a value (no void) And repeat