Primary:
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Sets:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Miscellaneous:
[2001S]
[98F]
[SamR]
[Glimmer Labs]
Summary:
In today's lab, you will explore the yacc parser
generator.
Collaboration: Feel free to work on this lab in pairs or trios.
Turning It In: Save your answers in a plain text file and submit it using the ECA.
Grading: I expect that you will gain more from doing this lab than from me grading this lab. I may simply scan through your answers to see if you had any particularly valuable insights.
Supporting Files:
As you've observed in class, once a grammar gets sufficiently complex,
it seems more efficient to build a program to translate the grammar to
a parser than to translate the grammar by hand. Early in the development
of Unix, one of the community (and I'll admit I've forgotten who),
developed yacc, yet another compiler compilers
.
Yacc builds parsers in C. Yacc also permits you to include actions
in your productions. These actions, essentially arbitrary pieces of C
code, are executed as part of the parse process.
Yacc files have a fairly simple format.
Yacc produces one procedure, yyparse, which parses
from standard input. yyparse(void) expects to be able to
call two procedures that you must provide, yylex(void)
(which reads input tokens) and yyerror(char *), which reports
errors.
Here's a simple yacc grammar (the center part) for anbm.
S : A B ; A : 'a' A | ; B : 'b' B | ;
Here's an extended version that prints out the rule applied at each step.
S : A B { printf("S -gt; A B\n"); }
;
A : 'a' A { printf("A -> a A\n"); }
| { printf("A -> epsilon\n"); }
;
B : 'b' B { printf("B -> b B\n"); }
| { printf("B -> epsilon\n"); }
;
And here's a variant that counts the number of a's and b's.
S : A B
;
A : 'a' { ++acount; } A
|
;
B : 'b' { ++bcount; } B
|
;
Because the Yacc program lacks a main and needs
yylex and yyerror, the C code at the end of
the Yacc file usually includes those three procedures.
Here's a simple yylex that skips carriage returns (which
experience suggests is a good idea, at least for testing).
int yylex() {
char ch;
while ((ch = getchar()) == '\n')
;
return ch;
}
Here's a simple yyerror that simply prints out the
errors message.
void yyerror(char *s) {
fprintf(stderr, "Boom: %s\n", s);
}
Finally, here's a main that simply calls yyparse.
int main() {
return yyparse();
}
You save your yacc code in a file ending in .y. You compile
a yacc file with
yacc file.y
By default, yacc produces the file y.tab.c. You can compile
that file with
cc y.tab.c
.
By default, cc produces the file a.out.
Make copies of the four sample grammars,
a. Look at ab-simple.y and make sure you understand all the
parts.
b. Compile ab-simple.y.
c. See what the compiled file does when given inputs that are and are not within the language.
a. Look at ab-prods.y and make sure you understand all the
parts.
b. Compile ab-prods.y.
c. See what the compiled file does when given inputs that are and are not within the language.
a. Look at ab-steps.y and make sure you understand all the
parts.
b. Compile ab-steps.y.
c. See what the compiled file does when given inputs that are and are not within the language. Do you observe anything interesting about the output?
a. Look at ab-count.y and make sure you understand all the
parts.
b. Compile ab-count.y.
c. See what the compiled file does when given inputs that are and are not within the language. Do you observe anything interesting about the output?
Write a Yacc program to parse our favorite language, anbn|ancn.
What problems, if any, did you encounter?
Write a Yacc program that converts infix arithmetic expressions to postfix.
Spring 2001 [Samuel A. Rebelsky]
Monday, 7 October 2002 [Samuel A. Rebelsky]
ab.y.
Primary:
[Skip To Body]
[Front Door]
[Current]
[Glance]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Sets:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Project]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Miscellaneous:
[2001S]
[98F]
[SamR]
[Glimmer Labs]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Dec 10 08:53:30 2002.
The source to the document was last modified on Mon Oct 14 15:01:40 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS362/2002F/Labs/lab.06.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby