1 / 10

Yacc Examples

Yacc Examples. 66.648 Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic. Lecture Outline. YACC program format Examples - These are in the course home page under Pgms/yaccpgms. YACC program format. It Consists of three parts as in the case of lex.

alamea
Télécharger la présentation

Yacc Examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Yacc Examples • 66.648 Compiler Design Lecture (01/28/98) • Computer Science • Rensselaer Polytechnic

  2. Lecture Outline • YACC program format • Examples - These are in the course home page under Pgms/yaccpgms

  3. YACC program format • It Consists of three parts as in the case of lex. • 1. Declarations • 2. Productions • 3. Programs

  4. Format • %{ • /* Parser Specs */ • #include ... • #define … • %} • %token STDK • %token STRING • %start Program

  5. Format Cont... • %% • Program: PROGRAMTK.. • ; • stmts: stmts stmt • | • ; • … • %%

  6. Format Cont... • %% • void yyerror(char *) { …} • void main(void) • { yyparse(); • }

  7. Declaration Section • Any C initialization that may be necessary for the parser’s interface with the rest of the system. • The specification of various language categories • or tokens. (Sometime certain precednces). • The specification of the left hand side of a particular production as the start symbol.

  8. Productions • This part is a specification of the grammar in LALR(1) of whatever we want to parse. • For your first project, the grammar will be that of Java. • It is essentail that the grammar is of LALR(1) - free of ambiguities - Otherwise, you will get error messages such as shift-reduce conflicts and/or reduce/reduce conflicts.

  9. Supporting Program Section • This is a third major part of the parser specification file. This optional section may contain a number of supporting C functions or compiler directives to include a file containing these functions. • The function yyerror(), that allows the user to specify action taken by the parser when a finite state machine enters an error state. • The parse alos requires that a scanner yylex() be provided. So, if you don’t use lex, you have to provide such a function.

  10. Compiling YACC Programs • yacc -d -v parser.y • (-d produces y.tab.h defining tokens as integer numbers. • -v produces y.output, the complete finite state machine description. -v can be omitted most often unless you are debugging the grammar itself. ) • This produces y.tab.c file. • To compile, • gcc y.tab.c -ll -ly • -ll and -ly are lex and yacc libraries.

More Related