9/15/17 CSCI 2320 Syntax MOHAMMAD T. IRFAN Review of defini@ons (see classnote) § Syntax (form): expressed by grammar § CFG/BNF: (1) terminal (2) nonterminal (3) producEon § SentenEal form § Sentence § Language § LeG-recursive vs. right-recursive producEon § DerivaEon & parse tree § LeG-most vs. right-most derivaEon § Ambiguous grammar: 2 parse trees for the same sentence 1
9/15/17 Resolving ambiguity: C++ C++ Grammar (on Blackboard) § “The else ambiguity is resolved by connecEng an else with the last encountered else-less if.” [Stroustrup, 1991] Resolving ambiguity: Java hXps://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.5 § Statement: IfThenStatement IfThenElseStatement ... Anything other than simple if- § IfThenStatement: then (known as short if) if ( Expression ) Statement § IfThenElseStatement: if ( Expression ) StatementNoShortIf else Statement 2
9/15/17 Resolving ambiguity: Algol 68 § By syntax of if-fi § Example: if (x > 0) if (y > 0) y := -5; What about else Python? y := 5; fi fi Extended BNF (EBNF) ITERATIONS, OPTIONS, AND CHOICES BNF IS AS POWERFUL AS EBNF (SEE CLASSNOTE) 3
9/15/17 Grammar for CLite CONCRETE SYNTAX (HIGHER LEVELS OF ABSTRACTION) LEXICAL SYNTAX (LOW LEVEL: TYPED THINGS) Precedence of operators in C Higher precedence hXp://en.cppreference.com/w/c/language/operator_precedence 4
9/15/17 Concrete Syntax (EBNF): Hierarchy of categories/abstrac@ons Program → int main ( ) { Declarations Statements } Declarations → { Declaration } Declaration → Type Identifier [ [ Integer ] ] { , Identifier [ [ Integer ] ] } ; Type → int | bool | float | char Statements → { Statement } Statement → ; | Block | Assignment | IfStatement | WhileStatement Block → { Statements } Assignment → Identifier [ [ Expression ] ] = Expression ; IfStatement → if ( Expression ) Statement [ else Statement ] WhileStatement → while ( Expression ) Statement Concrete Syntax (cont…) Expression → Conjunction { || Conjunction } Conjunction → Equality { && Equality } Equality → Relation [ EquOp Relation ] EquOp → == | != Relation → Addition [ RelOp Addition ] Note: Operators and RelOp → < | <= | > | >= punctuaEon symbols are part Addition → Term { AddOp Term } of "lexical syntax" (next) AddOp → + | - Term → Factor { MulOp Factor } MulOp → * | / | % Factor → [ UnaryOp ] Primary UnaryOp → - | ! Primary → Identifier [ [ Expression ] ] | Literal | ( Expression ) | Type ( Expression ) 5
9/15/17 Lexical Syntax Input : a stream of characters from the ASCII set, keyed by a programmer. Output : a stream of tokens or basic symbols, classified as follows: ◦ IdenEfiers e.g., Stack , x , i , push ◦ Literals e.g., 123 , 'x' , 3.25 , true ◦ Keywords bool char else false float if int main true while ◦ Operators = || && == != < <= > >= + - * / ! ◦ PunctuaEon ; , { } ( ) Lexical Syntax: Lexemes à Tokens Identifier → Letter { Letter | Digit } Letter → a | b | … | z | A | B | … | Z Digit → 0 | 1 | … | 9 Literal → Integer | Boolean | Float | Char Integer → Digit { Digit } Boolean → true | False Float → Integer . Integer Char → ‘ ASCII Char ‘ 6
9/15/17 Abstract Syntax Tree Parse Tree for z = x + 2*y; Fig. 2.9 7
9/15/17 Abstract Syntax Tree for z = x + 2*y; Fig. 2.10 Op@onal Reading Pages 49 -- 54 8
Recommend
More recommend