• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Calculator using LEX and YACC

June 21, 2011 by ProjectsGeek 3 Comments

Implementation of Calculator using LEX and YACC

Aim: Implementation of Calculator using LEX and YACC.

Objective: To study the process of lexical analysis and parsing.

Theory:

During the first phase the compiler reads the input and converts strings in the source to tokens. With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input. Each pattern specified in the input to lex has an associated action. Typically an action returns a token that represents the matched string for subsequent use by the parser. Initially we will simply print the matched string rather than return a token value.

The following represents a simple pattern, composed of a regular expression, that scans for identifiers. Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers.

letter(letter|digit)*

This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits. This example nicely illustrates operations allowed in regular expressions:

repetition, expressed by the “*” operator

alternation, expressed by the “|” operator

History of Lex & Yacc

�� Lex & Yacc were developed at Bell Laboratories in the 70’s

��Yacc was developed as the first of the two by Stephen C. Johnson

�� Lex was designed by Mike E. Lesk and Eric Schmidt to work with Yacc

�� Standard UNIX utilities

Lex & Yacc

��Programming Tools for writers of compilers and interpreters

��Also interesting for non-compilerwriters

��Any application looking for patterns in its input or having an

input/command language is acandiate for Lex/Yacc

  • lex and yacc help you write programs that transform structured input

– lex — generates a lexical analyzer

• divides a stream of input characters into meaningful units (lexemes), identifies them (token) and may pass the token to a parser generator, yacc

• lex specifications are regular expressions – yacc — generates a parser

• may do syntax checking only or create an interpreter

• yacc specifications are grammar components

Lex:

�� The Unix program “lex” is a “Lexical Analyzer Generator”

– Takes a high-level description of lexical tokens and actions

– Generates C subroutines that implement the lexical analysis

• The name of the resulting subroutine is “yylex”

�� Generally, yylex is linked to other routines, such as the parsing procedures

generated by YACC

  • Organization of a Lex program

%%

%%

�� Translation rules consist of a sequence of patterns associated with actions

�� Lex reads the file and generates a scanner

– Repeatedly locates the “longest prefix of the input that is matched by one or more of

the patterns”

– When the action is found, lex executes the associated action

– In the case of a tie:

• Use whichever regexp uses the most characters

• If same number of characters, the first rule wins

Regular Expressions in Lex

�� References to a single character

– x the character “x”

– “x” an “x”, even if x is an operator

– \x an “x”, even if x is an operator

– (x) an x

– [xy] the character x or y

– [x-z] the character x, y or z

– [ˆx] any character except x

– . any character except newline

�� Repetitions and options

– x? an optional x

– x* 0,1,2, … instances of x

– x+ 1,2,3, … instances of x

Yacc Introduction

�� Yacc is a theoretically complicated, but “easy” to use program that parses input files to

verify that they correspond to a certain language

�� Your main program calls yyparse() to parse the input file

�� The compiled YACC program automatically calls yylex(), which is in lex.yy.c

�� You really need a Makefile to keep it all straight

��Yacc takes a grammar that you specify (in BNF form) and produces a parser that

recognizes valid sentences in your language

��Can generate interpreters, also, if you include an action for each statement that is

executed when the statement is recognized (completed)

The Yacc Parser

�� Parser reads tokens; if token does not complete a rule it is pushed on a stack and the

parser switches to a new state reflecting the token it just read

�� When it finds all tokens that constitute the right hand side of a rule, it pops of the right

hand symbols from the stack and pushes the left hand symbol on the stack (called a

reduction)

�� Whenever yacc reduces a rule, it executes the user code associated with the rule

�� Parser is referred to as a shift/reduce parser

�� yacc cannot run alone — it needs lex

Organization of a Yacc file

��Definition section c

– Declarations of tokens used in grammar, the types of values used on the parser stack

and other odds and ends

– For example, %token PLUS, MINUS,

TIMES, DIVIDE

– Declaration of non-terminals, %union,etc

�� Rules section

– A list of grammar rules in BNF form

– Example:

– Each rule may or may not have an associated action (actions Communication between

Lex and Yacc

�� Whenever Lex returns a token to the parser, that has an associated value, the lexer

must store the value in the global variable yylval before it returns.

�� The variable yylval is of the type YYSTYPE; this type is defined in the file yy.tab.h

(created by yacc using the option ‘–d’).

�� By default it is integer.

�� If you want to have tokens of multiple valued types, you have to list all the values

using the %union declaration





Add Image


Add ImageAdd Image

Other Projects to Try:

  1. Lex and Yacc Calculator code using Unix Script
  2. Lexical analyzer Code in C Language
  3. Lexical Analyzer for Subset of C
  4. Regular Expression to DFA
  5. Software Design Laboratory Assignments

Filed Under: System Programming

Reader Interactions

Comments

  1. vidyasri.b.s says

    May 24, 2015 at 12:32 pm

    system software lab(lex and yacc , unix system programming) mini project lab ….. pls u help me

    Reply
    • ProjectsGeek says

      May 31, 2015 at 2:54 pm

      Please leave you message in comments, So that we can help you out.

  2. vika says

    April 10, 2015 at 1:40 pm

    Thanks!!! That helped!!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy