Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17.

Similar presentations


Presentation on theme: "Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17."— Presentation transcript:

1

2 Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17

3 Agenda. Reverse Polish Calculator. Semantic Values from Lex. Yacc program structure. Adding semantic actions. More Examples.

4 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.

5 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' ; %

6 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.

7 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.

8 RpCalc – Lex. digit [0-9] %{ #include #include "RpCalc.tab.h" void yyerror (char const*); %} % {digit}+("."{digit}*)?{ sscanf (yytext, "%lf", &yylval); return tNUM; } [ \t]{ }.|\n{ return *yytext; } % int yywrap () { return 1; }

9 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.

10 Yacc Program structure. %{... prologue... %}... %token definitions... %... grammar rules & actions... %... epilogue...

11 RpCalc - Declarations Section. %{ #define YYSTYPE double #include int yylex (void); void yyerror (char const *); %} %token tNUM

12 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; } ;

13 RpCalc - Epilogue Section. int main () { yyparse (); return 0; } void yyerror (char const *msg) { fprintf (stderr, "%s\n", msg); }

14 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.


Download ppt "Introduction To Yacc and Semantics © Allan C. Milne Abertay University v14.6.17."

Similar presentations


Ads by Google