SlideShare a Scribd company logo
1 of 46
Introducing of Bison 天官 2011-09-15 1
Flex and Bison statement:   NAME '=' expression expression: NUMBER '+' NUMBER 	   | NUMBER '-' NUMBER Flex: recognizes regular expressions. divides the input stream into pieces(token) terminal symbol: 	Symbols produced by the lexer are called terminal symbols or tokens nonterminal symbol: Those that are defined on the left-hand side of rules are called nonterminal symbols or nonterminals. VS Bison ,[object Object]
takes these pieces and groups them together logically.,[object Object]
Parsing methods Bison parsers can use either of two parsing methods, known as LALR(1) and GLR LALR(1) (Look Ahead Left to Right with a one-token lookahead), which is less powerful but considerably faster and easier to use than GLR. GLR (Generalized Left to Right). The most common kind of language that computer parsers handle is a context-free grammar(CFG) The standard form to write down a CFG is Baskus-Naur Form (BNF)
LR parser LR parser is a parser that reads input from Left to right and produces a Rightmost derivation.  The term LR(k) parser is also used; where the k refers to the number of unconsumed "look ahead" input symbols that are used in making parsing decisions. Usually k is 1 and the term LR parser is often intended to refer to this case. (LALR(1))
look ahead LALR(1) cannot deal with grammars that need more than one token of lookahead to tell whether it has matched a rule. phrase:	     cart_animal  AND CART 	  |  work_animal  AND PLOW cart_animal:     HORSE | GOAT work_animal:   HORSE | OX phrase:	     cart_animalCART 	  |  work_animalPLOW cart_animal:     HORSE | GOAT work_animal:   HORSE | OX Not support! OR phrase:	     cart_animal  AND CART 	  |  work_animal  AND PLOW cart_animal:     HORSE | GOAT work_animal:   OX
Rightmost Derivation Rule 1 expr ļƒž expr ā€“ digit exprļ‚®expr ā€“ digit exprļ‚®expr + digit exprļ‚® digit digit ļ‚®0|1|2|ā€¦|9 Example input:    3 + 8 - 2 The rightmost non-terminal is replaced in each step Rule 4 expr ā€“ digit ļƒž expr ā€“ 2 Rule 2 expr ā€“ 2ļƒž expr + digit - 2 Rule 4 expr + digit - 2 ļƒž expr + 8-2 Rule 3 expr + 8-2ļƒž digit + 8-2 Rule 4 digit + 8-2ļƒž3+8 -2
Leftmost Derivation Rule 1 expr ļƒž expr ā€“ digit The leftmost non-terminal is replaced in each step expr 1 1 Rule 2 expr ā€“ digit ļƒž expr + digit ā€“ digit 2 2 3 expr - digit Rule 3 expr + digit ā€“ digit ļƒž digit + digit ā€“ digit 3 5 4 expr digit + Rule 4 4 digit + digit ā€“ digitļƒž3 + digit ā€“ digit 2 Rule 4 3 + digit ā€“ digit ļƒž3 + 8 ā€“ digit 5 6 digit 8 Rule 4 3 + 8 ā€“ digit ļƒž3 + 8 ā€“ 2 6 3
Leftmost Derivation Rule 1 expr ļƒž expr ā€“ digit expr ļ‚® expr ā€“ digit expr ļ‚® expr + digit expr ļ‚® digit digit ļ‚®0|1|2|ā€¦|9 Example input:    3 + 8 - 2 The leftmost non-terminal is replaced in each step Rule 2 expr ā€“ digit ļƒž expr + digit ā€“ digit Rule 3 expr + digit ā€“ digit ļƒž digit + digit ā€“ digit Rule 4 digit + digit ā€“ digitļƒž3 + digit ā€“ digit Rule 4 3 + digit ā€“ digit ļƒž3 + 8 ā€“ digit Rule 4 3 + 8 ā€“ digit ļƒž3 + 8 ā€“ 2
Leftmost Derivation Rule 1 expr ļƒž expr ā€“ digit The leftmost non-terminal is replaced in each step expr 1 1 Rule 2 expr ā€“ digit ļƒž expr + digit ā€“ digit 6 2 2 expr - digit Rule 3 expr + digit ā€“ digit ļƒž digit + digit ā€“ digit 3 3 5 expr digit + Rule 4 4 digit + digit ā€“ digitļƒž3 + digit ā€“ digit 2 Rule 4 3 + digit ā€“ digit ļƒž3 + 8 ā€“ digit 5 4 digit 8 Rule 4 3 + 8 ā€“ digit ļƒž3 + 8 ā€“ 2 6 3
Context-Free Grammars A context-free grammar G is defined by the 4-tuple: G = (V, āˆ‘, R, S) where V is a finite set; each element  v Ļµ V is called a non-terminal character or a variable. Each variable represents a different type of phrase or clause in the sentence. Variables are also sometimes called syntactic categories. Each variable defines a sub-language of the language defined by . āˆ‘ is a finite set of terminals, disjoint from V, which make up the actual content of the sentence. The set of terminals is the alphabet of the language defined by the grammar G. R is a finite relation from V to (V U āˆ‘)*. The members of R are called the (rewrite) rules or productions of the grammar. S is the start variable (or start symbol), used to represent the whole sentence (or program). It must be an element of V. The asterisk represents the Kleene star operation.
Context-free language The language of grammar G = (V, āˆ‘, R, S) is the set 	L(G) = { Ļ‰Ļµ āˆ‘* : S Ļ‰Ļ‰ } 	A language L is said to be context-free languange(CFL), if there exists a CFG G, such that L = L(G).
Context-Free Grammars Comprised of A set of tokens or terminal symbols A set of non-terminal symbols A set of rules or productions which express the legal relationships between symbols A start or goal symbol Example: exprļ‚®expr ā€“ digit exprļ‚®expr + digit exprļ‚® digit digit ļ‚®0|1|2|ā€¦|9 ,[object Object]
Non-terminals: expr, digit
Start symbol: expr,[object Object]
Terms Symbols are strings of letters, digits, periods, and underscores that do not start with a digit. error is reserved for error recovery. Do not use C reserved words or bison's own symbols such as yyparse. Symbols produced by the lexer are called terminal symbols or tokens Those that are defined on the left-hand side of rules are called nonterminal symbols or nonterminals.
Structure of a Bison Specification ... definition section ... 	%% 	... rules section ... 	%% 	... user subroutines ...
Literal Block 	%{ 	... C code and declarations ... 	%} The contents of the literal block are copied verbatim to the generated C source file near the beginning, before the beginning of yypare(). Usually contains declarations of variables and functions, as well as #include. Bison also provides an experimental %code POS { ... } where POS is a keyword to suggest where in the generated parser the code should go.
Delaration %parse-param %require "2.4ā€œ  declare the minimum version of bison needed to compile it %start identifies the top-level rule (Named the first rule.) %union %token %type %left %right %nonassoc %expect
Token Define the ternimators. Bison treats a character in single quotes as a token Bison also allows you to decalre strings as aliases for tokens This defines the token NE and lets you use NE and != interchangeably in the parser. The lexer must still return the internal token values for NE when the token is read, not a string. expr: '(' expr ')'; %token NE "!=" %% ... expr:	expr "!=" exp;
Parse-param Normally, you call yyparse() with no arguments, if you need, youcan add parameters to its definition: %parse-param { char *modulename } 		%parse-param { int intensity } This allows you to call yyparse("mymodule", 42)
Type The %union declaration specifies the entire list of possible types %token is used for declaring token types %type is used for declaring nonterminal symbols %{ #include "calc.hā€œ      /* Contains definition of `symrec' */  %}  %union {  	double val;              /* For returning numbers. */  symrec *tptr;           /* For returning symbol-table pointers */  }  %token <tptr> VAR FNCT   /* Variable and Function */  %type <val> exp  %%
Structure of a Bison Specification 	... definition section ... 	%% ... rules section ... 	%% 	... user subroutines ...
Actions An action is C code executed when bison matches a rule in the grammar. The action can refer to the values associated with the symbols in the rule by using a dollar sign followed by a number. The name $$ refers to the value for the left-hand side (LHS) symbol. For rules with no action, bison uses a default of the following date: month '/' day '/' year	{ printf("date %d-%d-%d found", $1, $3, $5); } ; { $$ = $1; }
Rules Recursive Rules The action can refer to the values associated with the symbols in the rule by using a dollar sign followed by a number. In most cases, Bison handles left recursion much more efficiently than right recursion. numberlist  :	/* empty */                    |	numberlist NUMBER 	     ; exprlist: exprlist ',' expr;	/* left recursion */ or exprlist: expr ',' exprlist;	/* right recursion */
Special Characters %	All of the declarations in the definition section start with %. $	In actions, a dollar sign introduces a value reference. @	In actions, an @ sign introduces a location reference, such as @2 for the location of the second symbol in the RHS. '	Literal tokens are enclosed in single quotes. "	Bison lets you declare quoted string as parser alias for tokens. <>	In a value reference in an action, you can override the value's default type by enclosing the type name in angle brackets. {}	The C code in actions is enclosed in curly braces. ;	Each rule in the rules section should end with a semicolon. |	or syntax for multi-rules with same LHS. :	separate left-hand side and right-hand side -	Symbols may include underscores along with letters, digits, and periods. .	Symbols may include periods along with letters, digits, and underscores.
Reserved YYABORT In an action makes the parser routine yyparse() return immediately with a nonzero value, indicating failure. YYACCEPT In an action makes the parser routine yyparse() return immediately with a value 0, indicating success. YYBACKUP The macro YYBACKUP lets you unshift the current token and replace it with something else. sym:	TOKEN	{ YYBACKUP(newtok, newval); } It is extremely difficult to use YYBACKUP() correctly, so you're best off not using it.
Reserved yyclearin The macro yyclearin in an action discards a lookahead token if one has been read. It is most oftern useful in error recovery in an interactive parser to put the paarser into a known state after an error: YYDEBUG To include the trace code, either use the -t flag on the bison command line or else define the C preprocessor symbol YYDEBUG to be nonzero either on the C compiler command line or by inlcuding something like this in the definition section: stmtlist   :	stmt | stmtlist stmt; stmt	:	error { reset_input(); yyclearin; }; %{ #define YYDEBUG 1 %}
Ambiguity and Conflicts The grammar is truly ambiguous Shift/Reduce Conflicts Reduce/Reduce Conflicts The grammar is unambiguous, but the standard parsing technique that bison uses is not powerful enough to parse the grammar. (need to look more than one token ahead) We have already told about it of LALR(1).
Reduce/Reduce Conflicts A reduce/reduce conflict occurs when the same token could complete two different rules. 	%% 	prog:	proga | progb; 	proga:	'X'; 	progb:	'X';
Shift/Reduce Conflicts %type <a> exp ... %% ... expr: expr '+' exp          { $$ = newast('+', $1, $3); }        | expr '-' exp           { $$ = newast('-', $1, $3); }        | expr '*' exp           { $$ = newast('*', $1, $3); }        | expr '/' exp           { $$ = newast('/', $1, $3); }        | '|' exp           { $$ = newast('|', $2, NULL); }        | '(' exp ')'           { $$ = $2); }        | '-' exp           { $$ = newast('M', $2, NULL); }        | NUMBER { $$ = newnum($1); }        ; %% Example   2+3*4
Problem At this point, the parser looks at the * and could either reduce 2+3 using; to an expression or shift the *, expecting to be able to reduce: 	later on. 2		shift NUMBER E		reduce E->NUMBER E +		shift + E + 3		shift NUMBER E + E		reduce E->NUMBER Example   2+3 * 4 expr:    expr '+' exp expr:    expr ā€˜*' exp
Analysis The problem is that we haven't told bison about the precedence and associativity of the operators. Precedence controls which operators execute first in an expression. In and expression grammar, operators are grouped into levels of precedence from lowest to highest.The total number of levels depends on the language. The C language is notorious for having too many precedence levels, a total of 15 levels. Associativity controls the grouping of operators at the same precedence level.
Implicitly Solution %type <a> exp exp1 exp2 ... %% ... expr : expr1 '+' exp1 { $$ = newast('+', $1, $3); }         | expr1 '-' exp1 { $$ = newast('-', $1, $3); }         | expr1 { $$ = $1; } expr1: expr2 '*' exp2 { $$ = newast('*', $1, $3); }          | expr2 '/' exp2 { $$ = newast('/', $1, $3); }          | expr2 { $$ = $1; } expr2: '|' exp { $$ = newast('|', $2, NULL); }          | '(' exp ')' { $$ = $2); }          | '-' exp { $$ = newast('M', $2, NULL); }          | NUMBER { $$ = newnum($1); }          ; %%
Explicitly Solution %left '+' '-ā€™ %left '*' '/ā€™ %nonassoc '|' NMINUS %type <a> exp exp1 exp2 ... %% ... expr: expr '+' exp { $$ = newast('+', $1, $3); }        | expr '-' exp { $$ = newast('-', $1, $3); }        | expr '*' exp { $$ = newast('*', $1, $3); }        | expr '/' exp { $$ = newast('/', $1, $3); }        | '|' exp { $$ = newast('|', $2, NULL); }        | '(' exp ')' { $$ = $2); }        | '-' exp %prec UMINUS { $$ = newast('M', $2, NULL); }        | NUMBER { $$ = newnum($1); }        ; %%
Explicitly Solution %left, %right, and %nonassoc declarations defining the order of precedence from lowest to highest. %left, left associative %right, right associative %nonaccoc, no associativity UMINUS, pseudo token standing fro unary minus %prec UMINUS, %prec tells bison to use the precedence of UMINUS for this rule.
IF/THEN/ELSE conflict When Not to Use Precedence Rules In expression grammars and to resolve the "dangling else" conflict in grammars for if/then/else language constructs, it is easy to understand. But in other situations, it can be extremely difficult to understand. stmt:	IF '(' cond ')' stmt        |	IF '(' cond ')' stmt ELSE stmt        |	TERMINAL cond:	TERMINAL Ambiguous!!! IF ( cond ) IF ( cond ) stmt ELSE stmt Which one? IF ( cond ) { IF ( cond ) stmt  } ELSE stmt IF ( cond ) { IF ( cond ) stmt ELSE stmt }
Implicitly Solution stmt :matched        |	unmatched         ; matched  :other_stmt    |	IF expr THEN matched ELSE matched    ; unmatched  :	IF expr THEN stmt        |	IF expr THEN matched ELSE unmatched 			; other_stmt:	/* rules for other kinds of statement */ ... IF ( cond ) { IF ( cond ) stmt ELSE stmt }
Explicitly Solution %nonassoc	THEN %nonassoc	ELSE %% stmt  :	IF expr THEN stmt          |	IF expr stmt ELSE stmt          ; Equal to: %nonassoc LOWER_THAN_ELSE %nonassoc ELSE %% stmt  :	IF expr stmt %prec LOWER_THAN_ELSE          |	IF expr stmt ELSE stmt         ; IF ( cond ) { IF ( cond ) stmt ELSE stmt }
expect Occasionally you may have a grammar that has a few conflicts, you are confident that bison will resolve them the way you want, and it's too much hassle to rewrite the grammar to get rid of them. %expect N tells bison that your parser should have N shift/reduce conflicts. %expect-rr N to tell it how many reduce/reduce conflicts to expect.
Common Bugs In Bison Programs Infinite Recursion %% xlist:	xlist  ā€˜Xā€™ ; should be ==> %% xlist  :	'X' |	xlist  'Xā€™        ;
Common Bugs In Bison Programs Interchanging Precedence %token NUMBER %left PLUS %left MUL %% expr	:	expr PLUS expr %prec MUL 	|	expr MUL expr %prec PLUS 	|	NUMBER 	;
Lexical Feedback Parsers can sometimes feed information back to the lexer to handle otherwise difficult situations.  E.g. syntax like this: message ( any characters ) /* parser */ %{ 	init parenstring = 0; }% ... %% statement: MESSAGE { parenstring = 1; } '(' STRING ')';
Lexical Feedback /* lexer */ %{ 	extern int parenstring; %} %s PSTRING %% "message"	 return MESSAGE; "(" {     if(parenstring)  BEGIN PSTRING;     return '(';  } <PSTRING>[^)]* {     yylval.svalue = strdup(yytext);      BEGIN INITIAL;     return STRING;                             }
Structure of a Bison Specification 	... definition section ... 	%% 	... rules section ... 	%% ... user subroutines ...
User subroutines Section This section typically includes routines called from the actions. Nothing special.

More Related Content

What's hot

Function in c
Function in cFunction in c
Function in cRaj Tandukar
Ā 
Les fondamentaux du langage C
Les fondamentaux du langage CLes fondamentaux du langage C
Les fondamentaux du langage CAbdoulaye Dieng
Ā 
introduction to python
 introduction to python introduction to python
introduction to pythonJincy Nelson
Ā 
Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Shubham Shukla
Ā 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
Ā 
Circular linked list
Circular linked listCircular linked list
Circular linked listchauhankapil
Ā 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListManishPrajapati78
Ā 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
Ā 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonchanna basava
Ā 
Python For Data Science - French Course
Python For Data Science - French CoursePython For Data Science - French Course
Python For Data Science - French CourseHaytam EL YOUSSFI
Ā 
20 Facts about Swift programming language
20 Facts about Swift programming language20 Facts about Swift programming language
20 Facts about Swift programming languageRohit Tirkey
Ā 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked listFahd Allebdi
Ā 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
Ā 
Binary search python
Binary search pythonBinary search python
Binary search pythonMaryamAnwar10
Ā 
Linked list
Linked listLinked list
Linked listeShikshak
Ā 

What's hot (20)

Function in c
Function in cFunction in c
Function in c
Ā 
Les fondamentaux du langage C
Les fondamentaux du langage CLes fondamentaux du langage C
Les fondamentaux du langage C
Ā 
introduction to python
 introduction to python introduction to python
introduction to python
Ā 
Skip lists (Advance Data structure)
Skip lists (Advance Data structure)Skip lists (Advance Data structure)
Skip lists (Advance Data structure)
Ā 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Ā 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Ā 
Dijkstra
DijkstraDijkstra
Dijkstra
Ā 
Circular linked list
Circular linked listCircular linked list
Circular linked list
Ā 
Data Structure and Algorithms Linked List
Data Structure and Algorithms Linked ListData Structure and Algorithms Linked List
Data Structure and Algorithms Linked List
Ā 
Smart Pointers in C++
Smart Pointers in C++Smart Pointers in C++
Smart Pointers in C++
Ā 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
Ā 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
Ā 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
Ā 
Arithmetic Expression
Arithmetic ExpressionArithmetic Expression
Arithmetic Expression
Ā 
Python For Data Science - French Course
Python For Data Science - French CoursePython For Data Science - French Course
Python For Data Science - French Course
Ā 
20 Facts about Swift programming language
20 Facts about Swift programming language20 Facts about Swift programming language
20 Facts about Swift programming language
Ā 
Doubly linked list
Doubly linked listDoubly linked list
Doubly linked list
Ā 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Ā 
Binary search python
Binary search pythonBinary search python
Binary search python
Ā 
Linked list
Linked listLinked list
Linked list
Ā 

Similar to Introduction of bison

Module 11
Module 11Module 11
Module 11bittudavis
Ā 
Compiler Design File
Compiler Design FileCompiler Design File
Compiler Design FileArchita Misra
Ā 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
Ā 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Languagezone
Ā 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoobirbal
Ā 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming LanguageRaghavan Mohan
Ā 
Javascript
JavascriptJavascript
Javascriptguest03a6e6
Ā 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de JavascriptBernard Loire
Ā 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docxvenkatapranaykumarGa
Ā 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
Ā 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And OutputLISP Content
Ā 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Aman Sharma
Ā 
Token and operators
Token and operatorsToken and operators
Token and operatorsSamsil Arefin
Ā 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
Ā 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
Ā 

Similar to Introduction of bison (20)

Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Ā 
Module 11
Module 11Module 11
Module 11
Ā 
Compiler Design File
Compiler Design FileCompiler Design File
Compiler Design File
Ā 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
Ā 
The Java Script Programming Language
The  Java Script  Programming  LanguageThe  Java Script  Programming  Language
The Java Script Programming Language
Ā 
Javascript by Yahoo
Javascript by YahooJavascript by Yahoo
Javascript by Yahoo
Ā 
The JavaScript Programming Language
The JavaScript Programming LanguageThe JavaScript Programming Language
The JavaScript Programming Language
Ā 
Javascript
JavascriptJavascript
Javascript
Ā 
Les origines de Javascript
Les origines de JavascriptLes origines de Javascript
Les origines de Javascript
Ā 
Javascript
JavascriptJavascript
Javascript
Ā 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
Ā 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
Ā 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
Ā 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
Ā 
Ch3
Ch3Ch3
Ch3
Ā 
Assignment5
Assignment5Assignment5
Assignment5
Ā 
Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
Ā 
Token and operators
Token and operatorsToken and operators
Token and operators
Ā 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
Ā 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
Ā 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
Ā 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
Ā 
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdfssuser54595a
Ā 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
Ā 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
Ā 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
Ā 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
Ā 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
Ā 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
Ā 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
Ā 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
Ā 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
Ā 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
Ā 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
Ā 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
Ā 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
Ā 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
Ā 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Ā 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
Ā 
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAŠ”Y_INDEX-DM_23-1-final-eng.pdf
Ā 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
Ā 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
Ā 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
Ā 
Model Call Girl in Bikash Puri Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Bikash Puri  Delhi reach out to us at šŸ”9953056974šŸ”Model Call Girl in Bikash Puri  Delhi reach out to us at šŸ”9953056974šŸ”
Model Call Girl in Bikash Puri Delhi reach out to us at šŸ”9953056974šŸ”
Ā 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
Ā 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
Ā 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
Ā 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
Ā 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
Ā 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
Ā 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
Ā 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)ā€”ā€”ā€”ā€”IMP.OF KSHARA ...
Ā 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
Ā 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
Ā 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
Ā 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
Ā 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Ā 

Introduction of bison

  • 1. Introducing of Bison 天官 2011-09-15 1
  • 2.
  • 3.
  • 4. Parsing methods Bison parsers can use either of two parsing methods, known as LALR(1) and GLR LALR(1) (Look Ahead Left to Right with a one-token lookahead), which is less powerful but considerably faster and easier to use than GLR. GLR (Generalized Left to Right). The most common kind of language that computer parsers handle is a context-free grammar(CFG) The standard form to write down a CFG is Baskus-Naur Form (BNF)
  • 5. LR parser LR parser is a parser that reads input from Left to right and produces a Rightmost derivation. The term LR(k) parser is also used; where the k refers to the number of unconsumed "look ahead" input symbols that are used in making parsing decisions. Usually k is 1 and the term LR parser is often intended to refer to this case. (LALR(1))
  • 6. look ahead LALR(1) cannot deal with grammars that need more than one token of lookahead to tell whether it has matched a rule. phrase: cart_animal AND CART | work_animal AND PLOW cart_animal: HORSE | GOAT work_animal: HORSE | OX phrase: cart_animalCART | work_animalPLOW cart_animal: HORSE | GOAT work_animal: HORSE | OX Not support! OR phrase: cart_animal AND CART | work_animal AND PLOW cart_animal: HORSE | GOAT work_animal: OX
  • 7. Rightmost Derivation Rule 1 expr ļƒž expr ā€“ digit exprļ‚®expr ā€“ digit exprļ‚®expr + digit exprļ‚® digit digit ļ‚®0|1|2|ā€¦|9 Example input: 3 + 8 - 2 The rightmost non-terminal is replaced in each step Rule 4 expr ā€“ digit ļƒž expr ā€“ 2 Rule 2 expr ā€“ 2ļƒž expr + digit - 2 Rule 4 expr + digit - 2 ļƒž expr + 8-2 Rule 3 expr + 8-2ļƒž digit + 8-2 Rule 4 digit + 8-2ļƒž3+8 -2
  • 8. Leftmost Derivation Rule 1 expr ļƒž expr ā€“ digit The leftmost non-terminal is replaced in each step expr 1 1 Rule 2 expr ā€“ digit ļƒž expr + digit ā€“ digit 2 2 3 expr - digit Rule 3 expr + digit ā€“ digit ļƒž digit + digit ā€“ digit 3 5 4 expr digit + Rule 4 4 digit + digit ā€“ digitļƒž3 + digit ā€“ digit 2 Rule 4 3 + digit ā€“ digit ļƒž3 + 8 ā€“ digit 5 6 digit 8 Rule 4 3 + 8 ā€“ digit ļƒž3 + 8 ā€“ 2 6 3
  • 9. Leftmost Derivation Rule 1 expr ļƒž expr ā€“ digit expr ļ‚® expr ā€“ digit expr ļ‚® expr + digit expr ļ‚® digit digit ļ‚®0|1|2|ā€¦|9 Example input: 3 + 8 - 2 The leftmost non-terminal is replaced in each step Rule 2 expr ā€“ digit ļƒž expr + digit ā€“ digit Rule 3 expr + digit ā€“ digit ļƒž digit + digit ā€“ digit Rule 4 digit + digit ā€“ digitļƒž3 + digit ā€“ digit Rule 4 3 + digit ā€“ digit ļƒž3 + 8 ā€“ digit Rule 4 3 + 8 ā€“ digit ļƒž3 + 8 ā€“ 2
  • 10. Leftmost Derivation Rule 1 expr ļƒž expr ā€“ digit The leftmost non-terminal is replaced in each step expr 1 1 Rule 2 expr ā€“ digit ļƒž expr + digit ā€“ digit 6 2 2 expr - digit Rule 3 expr + digit ā€“ digit ļƒž digit + digit ā€“ digit 3 3 5 expr digit + Rule 4 4 digit + digit ā€“ digitļƒž3 + digit ā€“ digit 2 Rule 4 3 + digit ā€“ digit ļƒž3 + 8 ā€“ digit 5 4 digit 8 Rule 4 3 + 8 ā€“ digit ļƒž3 + 8 ā€“ 2 6 3
  • 11. Context-Free Grammars A context-free grammar G is defined by the 4-tuple: G = (V, āˆ‘, R, S) where V is a finite set; each element v Ļµ V is called a non-terminal character or a variable. Each variable represents a different type of phrase or clause in the sentence. Variables are also sometimes called syntactic categories. Each variable defines a sub-language of the language defined by . āˆ‘ is a finite set of terminals, disjoint from V, which make up the actual content of the sentence. The set of terminals is the alphabet of the language defined by the grammar G. R is a finite relation from V to (V U āˆ‘)*. The members of R are called the (rewrite) rules or productions of the grammar. S is the start variable (or start symbol), used to represent the whole sentence (or program). It must be an element of V. The asterisk represents the Kleene star operation.
  • 12. Context-free language The language of grammar G = (V, āˆ‘, R, S) is the set L(G) = { Ļ‰Ļµ āˆ‘* : S Ļ‰Ļ‰ } A language L is said to be context-free languange(CFL), if there exists a CFG G, such that L = L(G).
  • 13.
  • 15.
  • 16. Terms Symbols are strings of letters, digits, periods, and underscores that do not start with a digit. error is reserved for error recovery. Do not use C reserved words or bison's own symbols such as yyparse. Symbols produced by the lexer are called terminal symbols or tokens Those that are defined on the left-hand side of rules are called nonterminal symbols or nonterminals.
  • 17. Structure of a Bison Specification ... definition section ... %% ... rules section ... %% ... user subroutines ...
  • 18. Literal Block %{ ... C code and declarations ... %} The contents of the literal block are copied verbatim to the generated C source file near the beginning, before the beginning of yypare(). Usually contains declarations of variables and functions, as well as #include. Bison also provides an experimental %code POS { ... } where POS is a keyword to suggest where in the generated parser the code should go.
  • 19. Delaration %parse-param %require "2.4ā€œ declare the minimum version of bison needed to compile it %start identifies the top-level rule (Named the first rule.) %union %token %type %left %right %nonassoc %expect
  • 20. Token Define the ternimators. Bison treats a character in single quotes as a token Bison also allows you to decalre strings as aliases for tokens This defines the token NE and lets you use NE and != interchangeably in the parser. The lexer must still return the internal token values for NE when the token is read, not a string. expr: '(' expr ')'; %token NE "!=" %% ... expr: expr "!=" exp;
  • 21. Parse-param Normally, you call yyparse() with no arguments, if you need, youcan add parameters to its definition: %parse-param { char *modulename } %parse-param { int intensity } This allows you to call yyparse("mymodule", 42)
  • 22. Type The %union declaration specifies the entire list of possible types %token is used for declaring token types %type is used for declaring nonterminal symbols %{ #include "calc.hā€œ /* Contains definition of `symrec' */ %} %union { double val; /* For returning numbers. */ symrec *tptr; /* For returning symbol-table pointers */ } %token <tptr> VAR FNCT /* Variable and Function */ %type <val> exp %%
  • 23. Structure of a Bison Specification ... definition section ... %% ... rules section ... %% ... user subroutines ...
  • 24. Actions An action is C code executed when bison matches a rule in the grammar. The action can refer to the values associated with the symbols in the rule by using a dollar sign followed by a number. The name $$ refers to the value for the left-hand side (LHS) symbol. For rules with no action, bison uses a default of the following date: month '/' day '/' year { printf("date %d-%d-%d found", $1, $3, $5); } ; { $$ = $1; }
  • 25. Rules Recursive Rules The action can refer to the values associated with the symbols in the rule by using a dollar sign followed by a number. In most cases, Bison handles left recursion much more efficiently than right recursion. numberlist : /* empty */ | numberlist NUMBER ; exprlist: exprlist ',' expr; /* left recursion */ or exprlist: expr ',' exprlist; /* right recursion */
  • 26. Special Characters % All of the declarations in the definition section start with %. $ In actions, a dollar sign introduces a value reference. @ In actions, an @ sign introduces a location reference, such as @2 for the location of the second symbol in the RHS. ' Literal tokens are enclosed in single quotes. " Bison lets you declare quoted string as parser alias for tokens. <> In a value reference in an action, you can override the value's default type by enclosing the type name in angle brackets. {} The C code in actions is enclosed in curly braces. ; Each rule in the rules section should end with a semicolon. | or syntax for multi-rules with same LHS. : separate left-hand side and right-hand side - Symbols may include underscores along with letters, digits, and periods. . Symbols may include periods along with letters, digits, and underscores.
  • 27. Reserved YYABORT In an action makes the parser routine yyparse() return immediately with a nonzero value, indicating failure. YYACCEPT In an action makes the parser routine yyparse() return immediately with a value 0, indicating success. YYBACKUP The macro YYBACKUP lets you unshift the current token and replace it with something else. sym: TOKEN { YYBACKUP(newtok, newval); } It is extremely difficult to use YYBACKUP() correctly, so you're best off not using it.
  • 28. Reserved yyclearin The macro yyclearin in an action discards a lookahead token if one has been read. It is most oftern useful in error recovery in an interactive parser to put the paarser into a known state after an error: YYDEBUG To include the trace code, either use the -t flag on the bison command line or else define the C preprocessor symbol YYDEBUG to be nonzero either on the C compiler command line or by inlcuding something like this in the definition section: stmtlist : stmt | stmtlist stmt; stmt : error { reset_input(); yyclearin; }; %{ #define YYDEBUG 1 %}
  • 29. Ambiguity and Conflicts The grammar is truly ambiguous Shift/Reduce Conflicts Reduce/Reduce Conflicts The grammar is unambiguous, but the standard parsing technique that bison uses is not powerful enough to parse the grammar. (need to look more than one token ahead) We have already told about it of LALR(1).
  • 30. Reduce/Reduce Conflicts A reduce/reduce conflict occurs when the same token could complete two different rules. %% prog: proga | progb; proga: 'X'; progb: 'X';
  • 31. Shift/Reduce Conflicts %type <a> exp ... %% ... expr: expr '+' exp { $$ = newast('+', $1, $3); } | expr '-' exp { $$ = newast('-', $1, $3); } | expr '*' exp { $$ = newast('*', $1, $3); } | expr '/' exp { $$ = newast('/', $1, $3); } | '|' exp { $$ = newast('|', $2, NULL); } | '(' exp ')' { $$ = $2); } | '-' exp { $$ = newast('M', $2, NULL); } | NUMBER { $$ = newnum($1); } ; %% Example 2+3*4
  • 32. Problem At this point, the parser looks at the * and could either reduce 2+3 using; to an expression or shift the *, expecting to be able to reduce: later on. 2 shift NUMBER E reduce E->NUMBER E + shift + E + 3 shift NUMBER E + E reduce E->NUMBER Example 2+3 * 4 expr: expr '+' exp expr: expr ā€˜*' exp
  • 33. Analysis The problem is that we haven't told bison about the precedence and associativity of the operators. Precedence controls which operators execute first in an expression. In and expression grammar, operators are grouped into levels of precedence from lowest to highest.The total number of levels depends on the language. The C language is notorious for having too many precedence levels, a total of 15 levels. Associativity controls the grouping of operators at the same precedence level.
  • 34. Implicitly Solution %type <a> exp exp1 exp2 ... %% ... expr : expr1 '+' exp1 { $$ = newast('+', $1, $3); } | expr1 '-' exp1 { $$ = newast('-', $1, $3); } | expr1 { $$ = $1; } expr1: expr2 '*' exp2 { $$ = newast('*', $1, $3); } | expr2 '/' exp2 { $$ = newast('/', $1, $3); } | expr2 { $$ = $1; } expr2: '|' exp { $$ = newast('|', $2, NULL); } | '(' exp ')' { $$ = $2); } | '-' exp { $$ = newast('M', $2, NULL); } | NUMBER { $$ = newnum($1); } ; %%
  • 35. Explicitly Solution %left '+' '-ā€™ %left '*' '/ā€™ %nonassoc '|' NMINUS %type <a> exp exp1 exp2 ... %% ... expr: expr '+' exp { $$ = newast('+', $1, $3); } | expr '-' exp { $$ = newast('-', $1, $3); } | expr '*' exp { $$ = newast('*', $1, $3); } | expr '/' exp { $$ = newast('/', $1, $3); } | '|' exp { $$ = newast('|', $2, NULL); } | '(' exp ')' { $$ = $2); } | '-' exp %prec UMINUS { $$ = newast('M', $2, NULL); } | NUMBER { $$ = newnum($1); } ; %%
  • 36. Explicitly Solution %left, %right, and %nonassoc declarations defining the order of precedence from lowest to highest. %left, left associative %right, right associative %nonaccoc, no associativity UMINUS, pseudo token standing fro unary minus %prec UMINUS, %prec tells bison to use the precedence of UMINUS for this rule.
  • 37. IF/THEN/ELSE conflict When Not to Use Precedence Rules In expression grammars and to resolve the "dangling else" conflict in grammars for if/then/else language constructs, it is easy to understand. But in other situations, it can be extremely difficult to understand. stmt: IF '(' cond ')' stmt | IF '(' cond ')' stmt ELSE stmt | TERMINAL cond: TERMINAL Ambiguous!!! IF ( cond ) IF ( cond ) stmt ELSE stmt Which one? IF ( cond ) { IF ( cond ) stmt } ELSE stmt IF ( cond ) { IF ( cond ) stmt ELSE stmt }
  • 38. Implicitly Solution stmt :matched | unmatched ; matched :other_stmt | IF expr THEN matched ELSE matched ; unmatched : IF expr THEN stmt | IF expr THEN matched ELSE unmatched ; other_stmt: /* rules for other kinds of statement */ ... IF ( cond ) { IF ( cond ) stmt ELSE stmt }
  • 39. Explicitly Solution %nonassoc THEN %nonassoc ELSE %% stmt : IF expr THEN stmt | IF expr stmt ELSE stmt ; Equal to: %nonassoc LOWER_THAN_ELSE %nonassoc ELSE %% stmt : IF expr stmt %prec LOWER_THAN_ELSE | IF expr stmt ELSE stmt ; IF ( cond ) { IF ( cond ) stmt ELSE stmt }
  • 40. expect Occasionally you may have a grammar that has a few conflicts, you are confident that bison will resolve them the way you want, and it's too much hassle to rewrite the grammar to get rid of them. %expect N tells bison that your parser should have N shift/reduce conflicts. %expect-rr N to tell it how many reduce/reduce conflicts to expect.
  • 41. Common Bugs In Bison Programs Infinite Recursion %% xlist: xlist ā€˜Xā€™ ; should be ==> %% xlist : 'X' | xlist 'Xā€™ ;
  • 42. Common Bugs In Bison Programs Interchanging Precedence %token NUMBER %left PLUS %left MUL %% expr : expr PLUS expr %prec MUL | expr MUL expr %prec PLUS | NUMBER ;
  • 43. Lexical Feedback Parsers can sometimes feed information back to the lexer to handle otherwise difficult situations. E.g. syntax like this: message ( any characters ) /* parser */ %{ init parenstring = 0; }% ... %% statement: MESSAGE { parenstring = 1; } '(' STRING ')';
  • 44. Lexical Feedback /* lexer */ %{ extern int parenstring; %} %s PSTRING %% "message" return MESSAGE; "(" { if(parenstring) BEGIN PSTRING; return '('; } <PSTRING>[^)]* { yylval.svalue = strdup(yytext); BEGIN INITIAL; return STRING; }
  • 45. Structure of a Bison Specification ... definition section ... %% ... rules section ... %% ... user subroutines ...
  • 46. User subroutines Section This section typically includes routines called from the actions. Nothing special.