bison tutorial - university of toronto · 2017-12-03 · description of bison lalr(1) parser...
TRANSCRIPT
![Page 1: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/1.jpg)
Bison TutorialBison Tutorial
Plus a Quick Look at A2Plus a Quick Look at A2
![Page 2: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/2.jpg)
Recall: Compiler Recall: Compiler Components and Components and
Assignment BreakdownAssignment BreakdownAssignment 1:
Lexer
Assignment 2:Parser
Match syntax rules
Assignment 3:Parser
Build AST
Assignment 4:Code generation
Tokens and Values
Assembly Language
Input file
![Page 3: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/3.jpg)
NoteNote
Not everything discussed today will Not everything discussed today will be required for assignment #2be required for assignment #2
![Page 4: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/4.jpg)
Description of BisonDescription of Bison
LALR(1) parser generator under the LALR(1) parser generator under the GNU licenseGNU license
BisonContext Free
GrammarSpecification
(.y)
LALR(1)Parser
(.c)
![Page 5: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/5.jpg)
Layout of Bison FileLayout of Bison File(Look familiar?)(Look familiar?)
%{%{ProloguePrologue%}%}Bison declarationsBison declarations%%%%Grammar RulesGrammar Rules%%%%EpilogueEpilogue
![Page 6: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/6.jpg)
Calculator ExampleCalculator Example
Recall: We implemented a simple Recall: We implemented a simple number lexer in flexnumber lexer in flex
Now we can implement a parser Now we can implement a parser which will take actions on this filewhich will take actions on this file
![Page 7: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/7.jpg)
Bison PrologueBison Prologue Between “%{” and “%}”Between “%{” and “%}” Content between those is copied verbatim Content between those is copied verbatim
into output fileinto output file
%{%{#define YYSTYPE double#define YYSTYPE double#include <math.h>#include <math.h>#include <stdio.h>#include <stdio.h>int yylex(void);int yylex(void);void yyerror(char const*);void yyerror(char const*);
%}%}
![Page 8: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/8.jpg)
Bison DeclarationsBison Declarations
Specify tokens and precedenceSpecify tokens and precedence Lower the rule, the higher the Lower the rule, the higher the
precendenceprecendence Left associatitivity vs. right Left associatitivity vs. right
associativityassociativity
%token NUM%token NUM%left ‘-’ ‘+’%left ‘-’ ‘+’%left ‘*’ ‘/’%left ‘*’ ‘/’%left NEG%left NEG%right ‘^’%right ‘^’
![Page 9: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/9.jpg)
Grammar RulesGrammar Rules
Grammar rules have the formGrammar rules have the formresult: componentsresult: components
e.g.: e.g.: exp: exp: exp ‘+’ expexp ‘+’ exp Can be followed by braces to Can be followed by braces to
indicates actions to takeindicates actions to takee.g.: e.g.: exp: exp: exp ‘+’ exp {exp ‘+’ exp {
printf(“summing two numbers\n”);printf(“summing two numbers\n”);
}}
![Page 10: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/10.jpg)
Grammar RulesGrammar Rules
Can return a valueCan return a value
exp:exp:NUMNUM { $$ = $1;{ $$ = $1;}}
| exp ‘+’ exp| exp ‘+’ exp { $$ = $1 + $3;{ $$ = $1 + $3;}}
$$ is the result$$ is the result $n is the n-th term in the syntax rule$n is the n-th term in the syntax rule
![Page 11: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/11.jpg)
Grammar Rules for Grammar Rules for CalculatorCalculator
(minus, division, exp (minus, division, exp omitted)omitted)input: /* empty */input: /* empty */
| input line| input line;;line: line: ‘\n’‘\n’
| exp ‘\n’ | exp ‘\n’ { printf(“\t %.10g\n”, $1); }{ printf(“\t %.10g\n”, $1); };;exp:exp: NUMNUM { $$ = $1;{ $$ = $1; }}
| exp ‘-’ exp| exp ‘-’ exp { $$ = $1 - $3;{ $$ = $1 - $3; }}| exp ‘*’ exp| exp ‘*’ exp { $$ = $1 * $3; { $$ = $1 * $3; }}| ‘-’ exp %prec NEG| ‘-’ exp %prec NEG { $$ = -$2{ $$ = -$2 }}
;;
![Page 12: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/12.jpg)
PrecedencePrecedence
%prec indicates that the unary minus %prec indicates that the unary minus has the same precedence as NEGhas the same precedence as NEG Or second highest (recall our Or second highest (recall our
declarations)declarations)
![Page 13: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/13.jpg)
EpilogueEpilogue
Place helper functions here or the Place helper functions here or the main() functionmain() function
int main (void)int main (void)
{{
return yyparse();return yyparse();
}}
![Page 14: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/14.jpg)
CompilationCompilation
> bison parser.y> bison parser.y Outputs to parser.tab.cOutputs to parser.tab.c
> bison parser.y –o othername.c> bison parser.y –o othername.c Outputs to othername.cOutputs to othername.c
> bison parser.y –d> bison parser.y –d -d flag tells bison to also create a header -d flag tells bison to also create a header
file with macro definitionsfile with macro definitions Ouputs to parser.tab.c and parser.tab.hOuputs to parser.tab.c and parser.tab.h
![Page 15: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/15.jpg)
See Bison Run…See Bison Run…
![Page 16: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/16.jpg)
Data TypesData Types
Sometimes the result (i.e. $$) is a Sometimes the result (i.e. $$) is a floatfloat E.g. the calculatorE.g. the calculator
Sometimes the result can be Sometimes the result can be different valuesdifferent values E.g.E.g. str: str ‘+’ strstr: str ‘+’ str { $$ = plusStr($1, { $$ = plusStr($1,
$3); };$3); }; num: num ‘+’ num {$$ = num: num ‘+’ num {$$ =
plusNum($1,$3); }plusNum($1,$3); }
![Page 17: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/17.jpg)
Data TypesData Types
To accommodate thisTo accommodate this In the declarationsIn the declarations
%union {%union {
int val;int val;
char* str;char* str;
}} States that $$ can be either an int or a States that $$ can be either an int or a
char *char *
![Page 18: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/18.jpg)
Data TypesData Types
Declare the type of each tokenDeclare the type of each token%token <val> NUM%token <val> NUM NUM token is the same type as val NUM token is the same type as val
(which is int)(which is int)
%token <str> STRING%token <str> STRING STRING token is the same type as str STRING token is the same type as str
(which is char*)(which is char*)
![Page 19: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/19.jpg)
Error HandlingError Handling
Whenever yyparse() detect an error Whenever yyparse() detect an error yyerror() is calledyyerror() is called
Example of yyerror()Example of yyerror()void yyerror (char *const s) {void yyerror (char *const s) {
fprintf(stderr, “ERROR on line %d\n”, fprintf(stderr, “ERROR on line %d\n”, lineno);lineno);
}}
![Page 20: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/20.jpg)
On to On to Assignment 2Assignment 2
![Page 21: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/21.jpg)
Recall: Compiler Recall: Compiler Components and Components and
Assignment BreakdownAssignment BreakdownAssignment 1:
Lexer
Assignment 2:Parser
Match syntax rules
Assignment 3:Parser
Build AST
Assignment 4:Code generation
Tokens and Values
Assembly Language
Input file
![Page 22: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/22.jpg)
Assignment Two OptionsAssignment Two Options
Use Bison to create a parser for LR(1) Use Bison to create a parser for LR(1) grammargrammar
Implement a parser in C/C++ with Implement a parser in C/C++ with for an LL(k) grammarfor an LL(k) grammar
![Page 23: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/23.jpg)
Example Run…Example Run…
![Page 24: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/24.jpg)
ExpectationsExpectations
DocumentationDocumentation Discuss: Design, implementation, testingDiscuss: Design, implementation, testing
TestingTesting Cover: Normal, tricky and error casesCover: Normal, tricky and error cases The more rigorous the betterThe more rigorous the better
ImplementationImplementation Create the parser as describedCreate the parser as described Every failed test case cause marks to be Every failed test case cause marks to be
deducteddeducted
![Page 25: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/25.jpg)
AdviceAdvice
Use a macro so that parsing Use a macro so that parsing information is only output when –Tp information is only output when –Tp is setis set
![Page 26: Bison Tutorial - University of Toronto · 2017-12-03 · Description of Bison LALR(1) parser generator under the LALR(1) parser generator under the GNU license Bison Context Free](https://reader033.vdocuments.net/reader033/viewer/2022042711/5f756283820eab288a77c03d/html5/thumbnails/26.jpg)
SubmissionSubmission
Directory for starter2Directory for starter2 doc/doc/
Documentation notesDocumentation notes cases/cases/
Test casesTest cases other files: scanner.l, parser.y, etc.other files: scanner.l, parser.y, etc.
tar –zcvf starter2.tar.gz starter2tar –zcvf starter2.tar.gz starter2 submitcsc467f 1 starter2.tar.gzsubmitcsc467f 1 starter2.tar.gz