yacc background
play

YACC Background ! Review : Recall grammars for YACC are a CSCI: - PDF document

YACC Background ! Review : Recall grammars for YACC are a CSCI: 4500/6500 Programming variant of BNF Languages Can be used to express context free languages X -> p X is non terminal, p is a string of non-terminals and/ Conclusion of


  1. YACC Background ! Review : Recall grammars for YACC are a CSCI: 4500/6500 Programming variant of BNF Languages » Can be used to express context free languages X -> p » X is non terminal, p is a string of non-terminals and/ Conclusion of Lex and YACC and the or terminals) Theory behind them (today– focus on » Context free because X can be replaced by p YACC) regardless of the context that X is in. 1 2 Maria Hybinette, UGA Maria Hybinette, UGA Example: ‘ Generating ’ a String Some YACC Theory in this (not parsing a string – yet) Context ! Example: Grammar that multiply and adds numbers: » E ! E + E (rule 1) ! YACC - reduces an ‘ expression ’ to a single » E ! E * E (rule 2) non-terminal (the start symbol) » E ! id (rule 3) ! Is a bottom up or ‘ shift-reduce ’ parser (LR – ! id is returned by lex (returns terminals) and Parses Left to right, right-most). only appears on right hand side. » (L) Reads the string from left to right (like » x + y * z is generated by: westerners) and (R) produces the right-most derivations. E ! E * E (rule 2) ! E * z (rule 3) To Parse the Language we need to go in reverse of generating the grammar ! E + E * z (rule 1) ! E + y * z (rule 3) ! x + y * z (rule 3) 3 4 Maria Hybinette, UGA Maria Hybinette, UGA Now – How YACC Parses. A Conflict at Step 6 (Ambiguity) E ! E + E (rule 1) E ! E + E (rule 1) E ! E * E (rule 2) E ! E * E (rule 2) E ! id E ! id (rule 3) (rule 3) To parse the expression we go in reverse, reduce an expression to a single non ! To parse the expression we go in reverse, reduce an expression to a single non ! terminal, We do this by shift-reduce parsing and use a stack for storing the terms terminal, We do this by shift-reduce parsing and use a stack for storing terms 1) . x + y * z � shift (terms on stack are on the left of dot) � 1) . x + y * z � shift (stack on left of dot) � 2) x . + y * z � reduce (rule 3) � 2) x . + y * z � reduce (rule 3) � 3) E . + y * z � shift � 3) E . + y * z � shift � 4) E + . y * z � shift � 4) E + . y * z � shift � 5) E + y. * z � � reduce (rule 3) � 5) E + y. * z � � reduce (rule 3) � 6) E + E. * z � � shift � � shift (here it is choice – reduce ‘ E+E ’ or shift) � 6) E + E. * z � 7) E + E * . z � shift � 7) E + E * . z � shift � 8) E + E * z . � reduce (rule 3) emit multiply � 8) E + E * z . � reduce (rule 3) emit multiply � 9) E + E * E . � reduce (rule 2) emit add � 9) E + E * E . � reduce (rule 2) emit add � 10) E + E . � � reduce (rule 1) � 10) E + E . � � reduce (rule 1) � 11) E . � � Accept � 11) E . � � Accept � When we have a match on the stack to one of right hand side of ! “ shift reduce ” conflict at step 6 ambiguous grammar ! productions replace the match with the left hand side of token 5 6 Maria Hybinette, UGA Maria Hybinette, UGA

  2. Ambiguity Ambiguity Ambiguity means the parser can ’ t decide what ! This choice means we can ’ t construct a to do: unique parse tree for any string. ! Shift-Reduce Conflict: ! But what if we could direct the parser to » Can ’ t decide whether to shift or reduce a handle to a always prefer one choice over the other. non-terminal » Then ! Reduce-Reduce Conflict: – The parse tree would always be unique » Can ’ t decide whether to reduce to on or more non- – The grammar might even be smaller terminal. » How to resolve? E ! T – Rewriting the grammar OR E ! id – Indicate which operator has precedence (YACC T ! id enables this with the precedence definition) » Either reduces to E or to T 7 8 Maria Hybinette, UGA Maria Hybinette, UGA Ambiguity: What Does YACC Do? ! Conflict Resolution Defaults: ! Reflecting where we are ! and what we have » For shift-reduce conflicts YACC will always shift. done so far ! » For reduce-reduce conflict YACC selects the first rule. ! Jflap 9 10 Maria Hybinette, UGA Maria Hybinette, UGA Big Picture: Compilation Process Big Picture: Compilation Process Source program Source program Scanner Scanner Lexical Lexical Analyzer Analyzer Lexical units, token stream Lexical units, token stream Parser Parser Syntax Syntax Analyzer Analyzer Parse trees Parse tree Intermediate Code Generator Symbol Optimizer Table (optional) Semantic Analyzer Abstract syntax tree or other intermediate form Code Generator Code Generator Machine Language Machine Language Computer Computer 11 12 Maria Hybinette, UGA Maria Hybinette, UGA

  3. Syntax: Regular Expressions Big Picture: Compilation Process (Tokens) & Context Free Grammars Source program a = b + c * d ! Tokens: Described by regular expressions Scanner » First phase of compilation process converts strings/lexemes of Lexical the programming language to tokens (a representation of the Analyzer lexeme in the computer) id1 = id2 + id3 * id4 Lexical units, token stream – Example: letter ( letter | digit ) * » Can be generated from just three rules/operations: – Concatenation Parser – Repetition (arbitrary number of times - Kleene closure) Syntax Analyzer = – Alternation (Choice from a finite set) Parse tree id1 ! Context Free Language + * » Generated from 4 operations: id2 id4 – Concatenation id3 – Repetition (arbitrary number of times - Kleene closure) Code Generator – Alternation (Choice from a finite set) load id3 Machine/Assembly Language – Recursion mul id4 add id2 Computer store id1 13 14 Maria Hybinette, UGA Maria Hybinette, UGA Definition of Languages Parse Trees ! Recognizers ! Grammars describes ‘ hierarchical syntactic structures ’ so these can be “ represented ” by parse » Reads input string and accepts or rejects if the trees (e.g., a parser generates parse trees). string is in the language ! Idea: » Example: Parsers -- the syntax analyzer of a » To build a parse tree, put the start symbol at the root compiler (yacc- yet another compiler compiler) » Add children to every non-terminal, following any one of ! Generators the productions for that non-terminal in the grammar » Generate sentences of a language » Done when all the leaves are tokens » Example: Grammars are language generators » Read off leaves from left to right—that is the string derived by the tree 15 16 Maria Hybinette, UGA Maria Hybinette, UGA Example Grammar: <expr> ::= id | <number> | <expr> <op> <expr> | ( <expr> ) Example <op> ::= + | - | * | / <expr> ==> <expr> <op> <expr> ==> <expr> <op> <expr> expr> <expr> ==> <expr> <op> <expr> <expr> ==> <expr> <op> <expr> Grammar: ==> <expr> ==> <expr> <op> <op> id id ==> <expr> * <expr> ==> <expr> * <expr> <expr> ::= id | <number> | <expr> <op> <expr> | ( <expr> ) <op> ::= + | - | * | / ==> ==> <expr> <expr> + + id id ==> id * <expr> ==> id * <expr> ==> <expr> <op> ==> <expr> <op> <expr> <expr> + id + id ==> id * <expr> <op> ==> id * <expr> <op> <expr> <expr> ==> <expr> ==> <expr> <op> <op> id + id id + id ! Generated String: slope * x + intercept ==> id * <expr> + ==> id * <expr> + ==> ==> <expr> <expr> * id + id * id + id <expr> <expr> ==> id * id + id ==> id * id + id ==> ==> id id * id id + id id <expr> ==> <expr> <op> <expr> <expr> ==> <expr> <op> expr> Derivation and Sentenial form ==> <expr> ==> <expr> <op> <op> id id <expr> <expr> ==> ==> <expr> <expr> + + id id ==> <expr> <op> ==> <expr> <op> <expr> <expr> + id + id <expr> <op> <expr> <expr> <op> <expr> ==> <expr> ==> <expr> <op> <op> id + id id + id ==> ==> <expr> <expr> * id + id * id + id ==> id * id + id ==> id * id + id <expr> <op> <expr> <expr> <op> <expr> (slope) (x) (intercept) (slope) (x) (intercept) id id(slope) * id(x) + id(intercept) id * id + 17 18 Maria Hybinette, UGA Maria Hybinette, UGA

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend