Introduction to parsing

English examples

BNF notation

Context-free grammars

Bison grammars

	terminals and non-terminals
	tokens with values

How to call bison

  foo.y produces foo.c
  -y produces y.tab.c

Syntax of bison:

Definitions:

  Type of values
  %union {
    double floatval;
    int intval;
  }

  %token <intval> FOO

  %type of non-terminals

  %start defines start symbol

  -d writes foo.h with macros for token types and yylval

Actions:

name	expansion		{ do code }
        | expansion		{ do code }
        | expansion		{ do code }
        ;

Assign to $$ to set the value of NAME
In the expansion, each token is $1 to $n.

Call yyparse
  returns 0 at EOF
  returns 1 for failure
YYACCEPT
YYABORT

yylex must return 0 for EOF

Limitations:
  Amibiguous parse
  Context sensitivity
  LR (1)  needs one lookahead to know where you are
	Left Recursive
  LALR (1)  needs one lookahead relative to the sets of rules partially parsed
	    at one time.
	Look Ahead, Left Recursive

Assignment:

1. Write a grammar that uses your existing lexer for calculator input and
functions as a prefix calculator.  identifiers should cause an error
to occur; just do simple integer arithmetic.  

2. Now do a postfix one, with . to generate printing.

extra
begin writing a bison parser for C.  
