Compilers (CS362 2002F)

Lab: Parser Generators

Preliminaries

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:

Background

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 File Format

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.

Sample Grammars

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
  |
  ;

Sample C Code

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();
}

Compiling Yacc Files

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.

Exercises

Exercise 0: Preparation

Make copies of the four sample grammars,

Exercise 1: Acceptance

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.

Exercise 2: Reporting Productions

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.

Exercise 3: Reporting Parsing Steps

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?

Exercise 4: Reporting Form

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?

Exercise 5: Our Favorite Language

Write a Yacc program to parse our favorite language, anbn|ancn.

What problems, if any, did you encounter?

Exercise 6: From Infix to Postfix

Write a Yacc program that converts infix arithmetic expressions to postfix.

 

History

Spring 2001 [Samuel A. Rebelsky]

  • Designed and held first version of this lab.

Monday, 7 October 2002 [Samuel A. Rebelsky]

  • Created first writeup.
  • Created new variants of ab.y.

 

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 ; Valid CSS! ; Check with Bobby

Glimmer Labs: The Grinnell Laboratory for Interactive Multimedia Experimentation & Research
glimmer@grinnell.edu