Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 Syntax Analysis.

Similar presentations


Presentation on theme: "Chapter 4 Syntax Analysis."— Presentation transcript:

1 Chapter 4 Syntax Analysis

2 Content Overview of this chapter 4.1 Introduction
4.2 Context-Free Grammars 4.3 Writing a Grammar 4.4 Top-Down Parsing 4.5 Bottom-Up Parsing 4.6 Introduction to LR Parsing: Simple LR 4.7 More Powerful LR Parsers 4.8 Using Ambiguous Grammars 4.9 Parser Generators

3 4.8 Using Ambiguous Grammars

4 4.8 Using Ambiguous Grammars
Why use ambiguous grammars? Certain ambiguous grammars are quite useful 1. Provides a shorter, more natural specification 2. In isolating commonly occurring syntactic constructs for special-case optimization We specify disambiguating rules that allow only one parse tree for each sentence

5 4.8.1 Precedence and Associativity to Resolve Conflicts
Consider the ambiguous grammar (4.3) E-> E+E | E*E | (E) | id LR(0) items: (I7 and I8 have conflicts)

6 4.8.1 Precedence and Associativity to Resolve Conflicts
Using the precedence and associativity information for + and *, consider: id + id * id Parsing table:

7 4.8.2 The "Dangling-Else" Ambiguity
Consider again the grammar: rewrite as: LR(0) items: (shift/reduce conflict in l4)

8 4.8.3 Error Recovery in LR Parsing
Detect an error: Consults the parsing action table and finds an error entry (Never by consulting the goto table) Error Recovery in LR Parsing 1. Panic-mode: Eliminate the phrase containing the syntactic errors 2. Phrase-level recovery: Examine each error entry

9 4.8.3 Error Recovery in LR Parsing
Panic-mode: scan down the stack until a state s with a goto on a particular nonterminal A is found. Zero or more input symbols are then discarded until a symbol a is found that can legitimately follow A. The parser then stacks the state GOTO(SA, ) and resumes normal parsing. There might be more than one choice for the nonterminal A. Phrase-level : examining each error entry in the LR parsing table and deciding on the basis of language usage the most likely programmer error that would give rise to that error.

10 4.8.3 Error Recovery in LR Parsing
el: This routine is called from states 0, 2, 4 and 5, all of which expect the beginning of an operand. push state 3 (the goto of states 0, 2, 4 and 5 on id); issue diagnostic "missing operand."

11 4.9 Parser Generators

12 In this section, we 4.9 Parser Generators
Show how a parser generator can be used Use the LALR parser generator Yacc as the basis of our discussion

13 4.9.1 The Parser Generator Yacc
Creating an input/output translator with Yacc: A Yacc source program has three parts: declarations %% translation rules supporting C routines

14 4.9.1 The Parser Generator Yacc
The Declarations Part two sections: both optional 1. Ordinary C declarations: %{ and %} 2. Declarations of grammar tokens: %token DIGIT

15 4.9.1 The Parser Generator Yacc
The Translation Rules Part <head> : <body>1 {<semantic action>1} | <body>2 {<semantic action>2} | <body>n {<semantic action>n} ; The Supporting C-Routines Part The lexical analyzer yylex() produces tokens consisting of a token name and its associated attribute value. If a token name such as DIGIT is returned, the token name must be declared in the first section of the Yacc specification. The attribute value associated with a token is communicated to the parser through a Yacc-defined variable yylval.

16 4.9.1 The Parser Generator Yacc

17 4.9.2 Using Yacc with Ambiguous Grammars
Modify the Yacc specification as

18 4.9.2 Using Yacc with Ambiguous Grammars
Two rules of resolve conflicts : 1. A reduce/reduce conflict is resolved by choosing the conflicting production listed first 2. A shift/reduce conflict is resolved in favor of shift The tokens are given precedences in the order in which they appear in the declarations part, lowest first. Tokens in the same declaration have the same precedence.

19 4.9.3 Creating Yacc Lexical Analyzers with Lex
Lex was designed to produce lexical analyzers that could be used with Yacc Replace the routine yylex() #include “lex.yy.c”

20 Error recovery use a form of error productions:
4.9.4 Error Recovery in Yacc Error recovery use a form of error productions: 1. The user decides what "major“ nonterminals will have error recovery 2. The user adds to the grammar error productions of the form A -> error , error is a yacc reserved word. A is a major nonterminal and  is a string of grammar symbols, perhaps the empty string. 3. Yacc generates a parser from such a specification.

21 4.9.4 Error Recovery in Yacc On encountering an error, Yacc pops symbols from its stack until it finds the topmost state on its stack whose underlying set of items includes an item of the form A -> error . The parser then "shifts" a fictitious token error onto the stack, as though it saw the token error on its input.

22 4.9.4 Error Recovery in Yacc the parser starts popping symbols from its stack until it encounters a state that has a shift action on the token error. State 0 is such a state (in this example, it's the only such state) The parser shifts the token error onto the stack, and then proceeds to skip ahead in the input until it has found a newline character.

23 The end of Lecture07


Download ppt "Chapter 4 Syntax Analysis."

Similar presentations


Ads by Google