CS385, Theory of Computation
yacc example

(This page contains the files and commands that I used in the yacc example that I showed in class)

This page is located at http://www.cs.bc.edu/~alvarez/yacc/yaccexample.html

yacc input file

yaccinput.y

command line

yacc -tv yaccinput.y

resulting files

y.tab.c
y.output

compiling

gcc -o parser y.tab.c

running the parser

./parser

sample run

Below, the string "aaaabb\n" is first entered, which is a valid string of the language described by the grammar in the yaccinput.y file. This leads to an accepting computation (ending in state 2). When an extra "a\n" is entered, however, the computation continues and correctly detects a grammatically incorrect string:
Script started on Wed 11 Nov 2009 05:56:12 PM EST
[alvarez@cs yacc]$ ./parser
aaaabb
yydebug: state 0, reading 97 ('a')
yydebug: state 0, shifting to state 1
yydebug: state 1, reading 97 ('a')
yydebug: state 1, shifting to state 4
yydebug: state 4, reading 97 ('a')
yydebug: state 4, shifting to state 1
yydebug: state 1, reading 97 ('a')
yydebug: state 1, shifting to state 4
yydebug: state 4, reading 98 ('b')
yydebug: state 4, shifting to state 7
yydebug: state 7, reducing by rule 3 (T : 'a' 'a' 'b')
yydebug: after reduction, shifting from state 4 to state 8
yydebug: state 8, reading 98 ('b')
yydebug: state 8, shifting to state 9
yydebug: state 9, reducing by rule 2 (T : 'a' 'a' T 'b')
yydebug: after reduction, shifting from state 0 to state 3
yydebug: state 3, reading 10 ('\n')
yydebug: state 3, shifting to state 5
yydebug: state 5, reducing by rule 4 (E : '\n')
yydebug: after reduction, shifting from state 3 to state 6
yydebug: state 6, reducing by rule 1 (S : T E)
yydebug: after reduction, shifting from state 0 to state 2
a
yydebug: state 2, reading 97 ('a')
syntax error
yydebug: error recovery discarding state 2
yydebug: error recovery discarding state 0
[alvarez@cs yacc]$ exit
exit

Script done on Wed 11 Nov 2009 05:56:46 PM EST