introduction to yacc and semantics © allan c. milne abertay university v14.6.17
TRANSCRIPT
![Page 1: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/1.jpg)
Introduction ToYacc and Semantics
© Allan C. Milne
Abertay University
v14.6.17
![Page 2: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/2.jpg)
Agenda.
• Reverse Polish Calculator.
• Semantic Values from Lex.
• Yacc program structure.
• Adding semantic actions.
• More Examples.
![Page 3: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/3.jpg)
The RpCalc Example.
• This example implements an interactive calculator using reverse polish notation.– Reverse polish removes the need to consider operator
precedence issues.
• This version is taken from section 2.1 of the Bison manual.
• A calculator example is traditional!– it exemplifies simple tokens, number microsyntax and
obvious semantics.
![Page 4: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/4.jpg)
RpCalc – Yacc BNF.
%token tNUM
%%
input : input line| ;
line : '\n' | exp '\n';
exp : tNUM | exp exp '+' | exp exp '-' | exp exp '*' | exp exp '/' | exp exp '^' | exp 'n' ;
%%
![Page 5: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/5.jpg)
Semantic Values of Tokens.
• Tokens can have both
– a token type
• the kind of token; e.g. + repeat Identifier.
– a semantic Value
• the value denoted by the token; e.g. 123 myTotal.
• Simple tokens such as punctuation and keywords have only a type.
• Microsyntax tokens such as integers and identifiers have both a type and a value.
![Page 6: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/6.jpg)
So How Does Lex Handle this?
• The token type is an integer representing the kind of token found; e.g. ‘+’, integer or identifier.– Defined either by the character ASCII code or the
%token declarations in the Yacc program.– Returned by yylex();
• i.e. the return xxx; Lex action.
• The semantic value of microsyntax tokens is the actual value denoted by the input; e.g. 123, “fred”.– Returned in the variable ‘yylval ‘;
• set explicitly in the action code of the corresponding lex pattern.
– the type of yylval is defined in the Yacc program.
![Page 7: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/7.jpg)
RpCalc – Lex.
digit [0-9]
%{#include <stdio.h>#include "RpCalc.tab.h"void yyerror (char const*);%}
%%
{digit}+("."{digit}*)? { sscanf (yytext, "%lf", &yylval); return tNUM; }
[ \t] { }
.|\n { return *yytext; }
%%
int yywrap () { return 1; }
![Page 8: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/8.jpg)
Parsing And Semantics.
• When parsing determines that a syntactic entity has been found – it may initiate some relevant semantic actions.
• This is the approach taken in Yacc where
– semantic actions can be associated with a production of a rule.
![Page 9: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/9.jpg)
Yacc Program structure.
%{... prologue ... %}
... %token definitions...
%% ... grammar rules & actions ...
%% ... epilogue ...
![Page 10: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/10.jpg)
RpCalc - Declarations Section.
%{ #define YYSTYPE double
#include <math.h> #include <stdio.h>#include <ctype.h>
int yylex (void); void yyerror (char const *); %}
%token tNUM
![Page 11: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/11.jpg)
RpCalc – with Semantics.
%%
input : input line| ;
line : '\n' | exp '\n' { printf ("\t= %.10g\n", $1); };
exp : tNUM { $$=$1; }| exp exp '+' { $$ =$1 + $2; }…
…| exp exp '-' { $$ =$1 - $2; }| exp exp '*' { $$ = $1 * $2; }| exp exp '/' { $$ = $1 / $2; }| exp exp '^' { $$ = pow ($1, $2); }| exp 'n' { $$ = -$1; };
![Page 12: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/12.jpg)
RpCalc - Epilogue Section.
int main () { yyparse (); return 0;}
void yyerror (char const *msg) { fprintf (stderr, "%s\n", msg);}
![Page 13: Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17](https://reader036.vdocuments.us/reader036/viewer/2022082710/56649e925503460f94b97925/html5/thumbnails/13.jpg)
The Calc Example.
• A more usual infix operator calculator.– From section 2.2 of the Bison manual.– Refer to the handout listing.
• This introduces– infix operators with brackets;– left and right associativity;– Operator precedence;– %prec to define context-sensitive precedence.