0% found this document useful (0 votes)
612 views18 pages

Assignment in Automata Theory and Compiler Design

This document discusses the parser generator Yacc. It defines parsing as analyzing a text made of tokens to determine its grammatical structure according to a formal grammar. A parser checks syntax and builds a data structure from tokens generated by a lexical analyzer. Yacc allows specifying grammar rules that define symbols in terms of other symbols and tokens. Yacc turns such specifications into efficient parsers. Yacc specifications include grammar rules for rewriting non-terminals as terminals and tokens, along with C code actions.

Uploaded by

peacelife_sam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
612 views18 pages

Assignment in Automata Theory and Compiler Design

This document discusses the parser generator Yacc. It defines parsing as analyzing a text made of tokens to determine its grammatical structure according to a formal grammar. A parser checks syntax and builds a data structure from tokens generated by a lexical analyzer. Yacc allows specifying grammar rules that define symbols in terms of other symbols and tokens. Yacc turns such specifications into efficient parsers. Yacc specifications include grammar rules for rewriting non-terminals as terminals and tokens, along with C code actions.

Uploaded by

peacelife_sam
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

Assignment in Automata

Theory and Compiler Design


Topic: Parse Generator Yacc

Guided by: Ms. Ishrat madam

 
Submitted by:
Sampada Sitaphale BE06F06F018
Contents:
 Definition of Parsing
 Definition of Parser
 Overview of Parsing Process
 Types of Parsers
 Definition of Compiler-Compiler
 Parser Generators
 Definition of YACC
 YACC Specification
 YACC Grammar Rules
 Advantages of YACC
Parsing:

Parsing, or, syntactic analysis, is the


process of analyzing a text, made of a
sequence of tokens to determine its
grammatical structure with respect to a
given formal grammar.
Parser:
 A parser is one of the components in
an interpreter or compiler, which checks for correct
syntax and builds a data structure implicit in the input
tokens.
 Uses a separate lexical analyser to create tokens from
the sequence of input characters.
 Parsers may be programmed by hand or may be
automatically generated by a tool (such as Yacc) from a
grammar written in Backus-Naur form.
 Programming languages tend to be specified in terms of
a context-free grammar for a parser to be written for
them.
Overview of Parsing Process
Types of Parsers:
 Top-down parsing 
Some examples:
◦ Recursive descent parser
◦ LL parser (Left-to-right, Leftmost derivation)
◦ X-SAIGA - eXecutable SpecificAtIons of GrAmmars.
 Bottom-up parsing 
Some examples:
◦ Precedence parser
 Operator-precedence parser
 Simple precedence parser
◦ BC (bounded context) parsing
◦ LR parser (Left-to-right, Rightmost derivation)
 Simple LR (SLR) parser
 LALR parser
 Canonical LR (LR(1)) parser
 GLR parser
  
Compiler-Compiler:
 Compiler-Compiler or compiler generator is a tool
that creates a parser, interpreter, or compiler from some
form of formal description.

 Themost common form of compiler-compiler is


a parser generator, whose input is a grammar (usually
in BNF) of a programming language, and whose
generated output is the source code of a parser.

 Most commonly used Parser Generator: YACC


Parser Generators:
Parser Generator YACC
Parser Generator .net
Parser Generator c
Parser Generator java
Parser Generator comparison
Parser Generator python
Parser Generator php
Parser Generator for c
What is YACC?
YACC= Yet Another Compiler-Compiler

Provides a general tool for describing the input


to a computer program.

Yacc user specifies the structures of his input,


together with code to be invoked as each such
structure is recognized.

Yacc turns such a specification into a subroutine


that handles the input process.
YACC Specification:
A yacc specification is structured along the same lines
as a Lex specification.

 %{
 /* C declarations and includes */
 %}
 /* Yacc token and type declarations */
YACC Specification:
%%
/* Yacc Specification in the form of grammar rules */
symbol : symbols tokens
{ $$ = my_c_code($1); }
;
%%
/* C language program (the rest) */

 TheYacc Specification rules are the place where the


various tokens provided by lex are "glued" together.
Yacc Grammar Rules
Each grammar rule defines a symbol in terms of:
◦ other symbols
◦ tokens (or terminal symbols) which come
from the lexer.
Different YACC Rules are:
◦ Simple Rule
◦ Alternate Rule
◦ Recursive Rule
◦ Empty rule
Simple Rule
 Rule for a simple, executable menu-command.
 Ex. menu_item : LABEL EXEC
;
 This rule defines a non-terminal symbol, menu_item in
terms of the two tokens LABEL and EXEC.
 Tokens are "terminal symbols", because the parser does
not need to expand them any further.
 Conversely, menu_item is a "non-terminal symbol"
because it can be expanded into LABEL and EXEC.
Alternate Rule
 Yacc allows us to have, multiple alternate definitions
of menu_item,
 menu-item may also have a keyword  DEFAULT
appear between the label and the executable command.
 menu_item : LABEL EXEC
| LABEL DEFAULT EXEC
;
 Where DEFAULT has a terminal value (token) defined
for it.
Recursive Rule
 Recursive rule allows "one or more menu items.”
 Ex. menu_items : menu_item
| menu_items menu_item
;
 This can also be written as:

| menu_item menu_items
but, due to the internals of yacc, this builds a less
memory-efficient parser.
Empty Rule
 In case of a single menu_item, we can also accomodate
the optional DEFAULT keyword; by defining an empty
rule, like this:
 Ex. menu_item : LABEL default EXEC '\n'
;
default : /* empty */
| DEFAULT
;
 The comment /* empty */ is ignored by yacc, and can be
omitted.
Advantages of YACC:
A parser created using Lex quickly
becomes unmaintainable, as the number
of user-defined states tends to explode.
YACC can handle greater number of
user-defined states.
For inputs containing elements which are
context-sensitive, YACC has to be used.
◦ Ex. The '*' character in C
THANK YOU!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy