Yacc%20Examples - PowerPoint PPT Presentation

About This Presentation
Title:

Yacc%20Examples

Description:

This part is a specification of the grammar in LALR(1) of whatever we want to parse. ... that the grammar is of LALR(1) - free of ambiguities - Otherwise, you ... – PowerPoint PPT presentation

Number of Views:245
Avg rating:3.0/5.0
Slides: 11
Provided by: scie210
Learn more at: http://www.cs.rpi.edu
Category:
Tags: 20examples | lalr | yacc

less

Transcript and Presenter's Notes

Title: Yacc%20Examples


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 parsers 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 dont 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.
Write a Comment
User Comments (0)
About PowerShow.com