introduction to yacc and semantics © allan c. milne abertay university v14.6.17

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

Upload: eleanor-montgomery

Post on 30-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

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

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

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

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

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

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

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

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

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

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

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

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

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.