Lecture 9: More on Grammars Dr Kieran T. Herley Department of Computer Science University College Cork 2016/17 KH (11/10/16) Lecture 9: More on Grammars 2016/17 1 / 1
Expressive Power of CFGs Not every language is describable using CFG notation; e.g. {a n b n c n : n 0} But syntax of most programming languages can be captured by CFG and are generally designed with this in mind Many aspects of programming language syntax stem from this KH (11/10/16) Lecture 9: More on Grammars 2016/17 2 / 1
Extended Notations Backus-Naur Notation BNF Essentially our CFG notation: non-terminals in <>, terminals without 1 EBNF: Extended BNF Grammar metasymbols {α} mean zero or more occurrences of α Example: term factor { ( * / ) factor } means term can be replaced by factor followed by zero or more factor s separated by + s and - s. Terminals ater shown in red to distinguish terminal ( from metasymbol (. 1 But BNF traditionally uses ::= symbol instead of. KH (11/10/16) Lecture 9: More on Grammars 2016/17 3 / 1
Extended Notations EBNF Grammar for Simple Arithmetic Expressions expr term { ( + - ) term } term factor { ( * / ) factor } factor NUM ID ( expr ) KH (11/10/16) Lecture 9: More on Grammars 2016/17 4 / 1
Extended Notations Syntax Diagrams Diagrammatic representation occasionally used to summarize programming language syntax term factor { ( * / ) factor } Essentially one diagram per production. Arguably more human-friendly than BNF, but less useful for compiler tools as we will see later on KH (11/10/16) Lecture 9: More on Grammars 2016/17 5 / 1
Ambiguity Ambiguity Some grammars are ambiguous string string + string string string - string string 0 1 9 Allows two different parse-trees/derivations for some strings 2 Ambiguity is generally undesirable for programming-language grammars: allows alternative interpretations of what code means. 2 Technically sentences will have distinct right-derivations: derivation in which rightmost nonterminal is the one replaced at each stage. KH (11/10/16) Lecture 9: More on Grammars 2016/17 6 / 1
Ambiguity Example string string + string string string - string string 0 1 9 9 5 2? KH (11/10/16) Lecture 9: More on Grammars 2016/17 7 / 1
Ambiguity Another Example S S + S S S * S S ( S ) S a Here a stand for any integer literal KH (11/10/16) Lecture 9: More on Grammars 2016/17 8 / 1
Ambiguity Why Does It Matter? Ambiguity can cause complicate our attempt to understand the meaning of a string May get two different meanings or interpretations for ambiguous string: KH (11/10/16) Lecture 9: More on Grammars 2016/17 9 / 1
Ambiguity The Dangling Else Problem statement if-stmt other if-stmt if ( exp ) statement if ( exp ) statement else statement exp 0 1 Consider if (0) if (1) other else other Dilemma: to which if does the else belong? KH (11/10/16) Lecture 9: More on Grammars 2016/17 10 / 1
Ambiguity Two Possible Parse Trees Which is the correct interpretation for if (0) if (1) other else other? Some languages live with the ambiguity by adopting the rule that the else matches the most closely nested if not yet coupled with an else (i.e. second tree) KH (11/10/16) Lecture 9: More on Grammars 2016/17 11 / 1
Ambiguity Interpretation 1 if (0) if (1) other else other? Else attaches to outer if KH (11/10/16) Lecture 9: More on Grammars 2016/17 12 / 1
Ambiguity Interpretation 2 if (0) if (1) other else other Else attaches to inner if KH (11/10/16) Lecture 9: More on Grammars 2016/17 13 / 1
Ambiguity Aside Can also re-engineer grammar to work around the problem statement matched-stmt unmatched-stmt matched-stmt if ( exp ) matched-stmt else matched-stmt unmatched-stmt if ( exp ) statement if ( exp ) matched-stmt else unmatched-stmt exp 0 1 KH (11/10/16) Lecture 9: More on Grammars 2016/17 14 / 1
Grammar for Tiny Tiny Grammar program stmtseq stmtseq stmtseq ; statement statement statement ifstmt repeatstmt assignstmt readstmt writestmt Note: i) program as sequence of statements; (ii) each individual statement can be of be of five differnet varieties KH (11/10/16) Lecture 9: More on Grammars 2016/17 15 / 1
Grammar for Tiny Sequences Production stmtseq stmtseq ; statement statement Form stmtseq we can derive statement statement ; statement statement ; statement ; statement The grammar captures idea that semicolons separate statements KH (11/10/16) Lecture 9: More on Grammars 2016/17 16 / 1
Grammar for Tiny Tiny Grammar cont d ifstmt if exp then stmtseq end if exp then stmtseq else stmtseq end repeatstmt repeat stmtseq until exp assignstmt identifier := exp readstmt read identifier writestmt write exp KH (11/10/16) Lecture 9: More on Grammars 2016/17 17 / 1
Grammar for Tiny Dangling Else? ifstmt if exp then stmtseq end if exp then stmtseq else stmtseq end KH (11/10/16) Lecture 9: More on Grammars 2016/17 18 / 1
Grammar for Tiny Tiny Grammar cont d exp simple-expr comp-op simple-expr simple-expr comp-op < = simple-expr simple-expr addop term term addop + - term term mulop factor factor mulop * / factor ( exp ) number identifier KH (11/10/16) Lecture 9: More on Grammars 2016/17 19 / 1
Grammar for Tiny Grammar for Java cont d Can find grammars for most programming languages e.g. [Java Grammar] 3 Can use these to facilitate generation of parsers 3 Google bnf grammar for java KH (11/10/16) Lecture 9: More on Grammars 2016/17 20 / 1
Grammar for Tiny Some Tiny Examples test1.tny write 17 test2.tny read x; write x fact.tny { Sample program in TINY language computes factorial } read x; { input an integer } if 0 < x then { don t compute if x 0 } fact := 1; repeat fact := fact x; x := x 1 until x = 0; write fact { output factorial of x } end KH (11/10/16) Lecture 9: More on Grammars 2016/17 21 / 1
Grammar for Tiny Tiny Example 1 test1.tny write 17 KH (11/10/16) Lecture 9: More on Grammars 2016/17 22 / 1
Grammar for Tiny Tiny Example 2 test2.tny read x; write x KH (11/10/16) Lecture 9: More on Grammars 2016/17 23 / 1
Grammar for Tiny Tiny Example 3 KH (11/10/16) Lecture 9: More on Grammars 2016/17 24 / 1
CFGs and Pushdown Automata Fact For every regular expression there is an equivalent finite automaton and vice versa. Fact For every context-free grammar there is an equivalent pushdown automaton and vice versa. KH (11/10/16) Lecture 9: More on Grammars 2016/17 25 / 1
CFGs and Pushdown Automata Pushdown Automata Next Step dictated by next input symbol top stack symbol Each Step involves change state of finite control push/pop symbol on/off stack advance one symbol in input KH (11/10/16) Lecture 9: More on Grammars 2016/17 26 / 1