18
Assignment in Assignment in Automata Theory and Automata Theory and Compiler Design Compiler Design Topic: Parse Generator Yacc Guided by: Ms. Ishrat madam Submitted by: Sampada Sitaphale BE06F06F018

Parser Generator YACC

Embed Size (px)

DESCRIPTION

This is the presentation on YACC the parser generator

Citation preview

Page 1: Parser Generator YACC

Assignment in Assignment in Automata Theory and Automata Theory and Compiler Design Compiler Design

Topic: Parse Generator Yacc

Guided by: Ms. Ishrat madam

 Submitted by:Sampada Sitaphale BE06F06F018

Page 2: Parser Generator YACC

Contents:Contents:Definition of ParsingDefinition of ParserOverview of Parsing ProcessTypes of ParsersDefinition of Compiler-CompilerParser GeneratorsDefinition of YACCYACC SpecificationYACC Grammar RulesAdvantages of YACC

Page 3: Parser Generator YACC

Parsing: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.

Page 4: Parser Generator YACC

Parser: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.

Page 5: Parser Generator YACC

Overview of Parsing Overview of Parsing ProcessProcess

Page 6: Parser Generator YACC

Types of Parsers: 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

 

Page 7: Parser Generator YACC

Compiler-Compiler:Compiler-Compiler:

Compiler-Compiler or compiler generator is a tool that creates a parser, interpreter, or compiler from some form of formal description.

The most 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

Page 8: Parser Generator YACC

Parser Generators:Parser Generators:Parser Generator YACCParser Generator .netParser Generator cParser Generator javaParser Generator comparisonParser Generator pythonParser Generator phpParser Generator for c

Page 9: Parser Generator YACC

What is YACC?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.

Page 10: Parser Generator YACC

YACC Specification:YACC Specification:A yacc specification is structured along the

same lines as a Lex specification.

%{ /* C declarations and includes */%} /* Yacc token and type declarations */

Page 11: Parser Generator YACC

YACC Specification:YACC Specification:%%/* Yacc Specification in the form of grammar rules */

symbol : symbols tokens { $$ = my_c_code($1); }

;%%

/* C language program (the rest) */

The Yacc Specification rules are the place where the various tokens provided by lex are "glued" together.

Page 12: Parser Generator YACC

Yacc Grammar RulesYacc 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

Page 13: Parser Generator YACC

Simple RuleSimple RuleRule 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.

Page 14: Parser Generator YACC

Alternate RuleAlternate RuleYacc 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.

Page 15: Parser Generator YACC

Recursive RuleRecursive 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.

Page 16: Parser Generator YACC

Empty RuleEmpty 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.

Page 17: Parser Generator YACC

Advantages of YACC: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

Page 18: Parser Generator YACC

THANK YOU!THANK YOU!