Presentation is loading. Please wait.

Presentation is loading. Please wait.

Some CFL Practicalities

Similar presentations


Presentation on theme: "Some CFL Practicalities"— Presentation transcript:

1 Some CFL Practicalities
CPSC 388 Ellen Walker Hiram College

2 Grammar for Expressions
Exp -> Exp Op Exp | ( Exp ) | number Op -> + | – | * Non-terminals: Exp, Op Terminals: number, +, –, *, (, ) Terminals are tokens not necessarily characters!

3 Simple Grammar for Statement
Stmt -> If-Stmt | … other statements If-Stmt -> if ( Exp ) Stmt | if ( Exp ) Stmt else Stmt Exp -> … on previous slide

4 More Concise If-Stmt using 
If-Stmt -> if ( Exp ) Stmt ElsePart ElsePart -> else Stmt | 

5 Epsilon Placement is Significant
How do these languages differ? L1: S -> x ; S | x L2: S-> x ; S |  L3: S-> x ; S | A A -> x |  L4: S-> N |  N -> x ; N | x L1: x;x x;x;x x;x;x;x etc L2: empty, x, x; , x;x x;x;

6 Recursion in Grammars When a rule refers to its own LHS in the RHS, the rule is recursive Left-recursion (LHS is first) Right-recursion (LHS is last) “Exp -> Exp Op Exp” is both left-recursive and right-recursive!

7 Why Recursion? Without recursion, languages must be finite
All possible derivations can be enumerated in advance Example: S-> aB B -> b | Cc C -> c

8 Recursion in Multiple Rules
Stmt -> If-Stmt | … other statements If-Stmt -> if ( Exp ) Stmt | if ( Exp ) Stmt else Stmt Stmt => If-Stmt => if … Stmt … There is a recursion on Stmt, but it takes 2 rules

9 Ambiguous Grammars If there is a string that has two different derivations (with different parse trees), the grammar is ambiguous. Only one string is needed to determine ambiguity

10 Exp Grammar is Ambiguous
Exp -> Exp Op Exp | ( Exp ) | number Op -> + | – | * Exp Exp Exp Op Exp Exp Op Exp Exp Op Exp Exp Op Exp number + number * number number + number * number

11 Dangling Else // w true when x!=0 and y=0 If (x!=0) if (y!=0) z=true;

12 Disambiguating Rule Leave the grammar ambiguous, but create a rule (outside the grammar) that determines which competing parse is “correct” Examples: Multiplication has precedence over addition Subtraction is left-associative Else matches the closest “if”

13 Modifying the Grammar Create additional non-terminals and rules that prevent ambiguity In this case, the syntax is (again) reflected only by the grammar

14 Exp Grammar with Precedence
Exp -> Exp Adop Exp | Term Term -> Term Mulop Factor | Factor Factor -> ( Exp ) | number Adop -> + | – Mulop -> * * evaluation forced “deeper” into the tree!

15 If Grammar with “Closest else”
Stmt -> MatchedStmt | UnmStmt MatchedStmt -> if (Exp) MatchedStmt else MatchedStmt | Other UnmStmt -> if (Exp) Stmt | if (Exp) MatchedStmt else UnmStmt Other -> { Stmt-list } | … |  Stmt-list -> Stmt Stmt-list | Stmt This is sufficiently awkward that real grammars will likely use the disambiguating rule solution

16 Avoiding the IF ambiguity
Require the else part in all if’s This is the LISP solution Require bracketing keywords If-stmt -> if Exp Stmt endif | if Exp Stmt else Stmt endif In some languages “fi” is used instead of “endif”

17 Abstract Parse Trees Design trees that include only essential aspects of the language (for semantics); remove “syntactic sugar” and intermediate non-terminals

18 Parse Tree vs. Abstract Parse Tree
Exp Exp Op Exp + Exp Op Exp * number + number * number number number number

19 Extended BNF “Ordinary BNF” is simply a CFG Extensions
Non-terminals named like: <expression> Terminals are bare sequences of characters Extensions { x } Repeat x 0 or more times ( x*) [ x ] x is optional (x | )


Download ppt "Some CFL Practicalities"

Similar presentations


Ads by Google