Finish Yacc, Names, Scope, and Binding




CS152

Chris Pollett

Sep 22, 2021

Outline

Introduction

Yacc $ variables

Yacc refers to parts of a rule using variables which begin with a dollar sign:

expression : expression '+' expression {$$ = $1 + $3;}
        | expression '-' expression {$$ = $1 - $3;}
	    | NUMBER {$$ =$1;}
        ;

$$ refers to the left hand side of the rule value. $n refers to the nth item on the right hand side.

Typing Tokens

More on Typed Tokens

To set up YYSTYPE in your grammar (will appear in y.tab.h file after yacc'ing):

%{
//stuff
%}
%union {
      double dval; // in this case we have two possibilities
      int ival; // could have more. In real world possibilities
                   // would include a struct for a syntax tree.
}
%token <ival> INTEGER
%token <dval> DOUBLE
%type <dval> expression /*notice can say type of nonterminal */

Typed Tokens and the Lexer

Error Handling in Your Grammar

In-Class Exercise

Names, Scopes, and Binding

Binding Time

Object Lifetime and Storage Management

Storage Allocation