parser generator yacc

Post on 17-Nov-2014

214 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

This is the presentation on YACC the parser generator

TRANSCRIPT

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

Contents:Contents:Definition of ParsingDefinition of ParserOverview of Parsing ProcessTypes of ParsersDefinition of Compiler-CompilerParser GeneratorsDefinition of YACCYACC SpecificationYACC Grammar RulesAdvantages of 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.

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.

Overview of Parsing Overview of Parsing ProcessProcess

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

 

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

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

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.

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 */

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.

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

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.

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.

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.

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.

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

THANK YOU!THANK YOU!

top related