0% found this document useful (0 votes)
57 views4 pages

YACC Syntax & Calculator Program

This document provides an overview of developing a calculator program using YACC. It discusses the structure of a YACC file, common functions and variables used including yyerror(), yyparse(), and yylex(). It also outlines the algorithm for a simple calculator program including declaring token values and precedence rules, forming parsing rules for operators, and initially calling yyparse() which calls yylex() to obtain tokens and evaluate expressions. The conclusion is that a program for a simple calculator was successfully studied and implemented using YACC.

Uploaded by

poopapule
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 DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views4 pages

YACC Syntax & Calculator Program

This document provides an overview of developing a calculator program using YACC. It discusses the structure of a YACC file, common functions and variables used including yyerror(), yyparse(), and yylex(). It also outlines the algorithm for a simple calculator program including declaring token values and precedence rules, forming parsing rules for operators, and initially calling yyparse() which calls yylex() to obtain tokens and evaluate expressions. The conclusion is that a program for a simple calculator was successfully studied and implemented using YACC.

Uploaded by

poopapule
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 DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

ASSIGNMENTNO.

:12

TITLE: AssignmenttounderstandbasicsyntaxofYACCspecification,builtinfunctions andvariablesandtodevelopaprogramforcalculator. PROBLEMSTATEMENT: WriteaprogramusingYACCforsimpledeskcalculator.

THEORY:
INTRODUCTION: YACC The UNIX utility yacc (Yet Another Compiler) parses a stream of token, typically generatedbylex,accordingtoauserspecifiedgrammar.

Structureofa yacc file:


Ayaccfilelooksmuchlikealexfile: %{

...definitions... %} %% ...rules... %% ...subroutines...

Definitions:Allcodebetween%{and%}iscopiedtothebeginningoftheresultingC file. Rules:Anumberofcombinationsofpatternandaction:iftheactionismorethana singlecommanditneedstobeinbraces. Code:Thiscanbeveryelaborate,butthemainingredientisthecalltoyylex,the lexicalanalyzer.Ifthecodesegmentisleftout,adefaultmainisusedwhichonlycalls yylex.

FUNCTIONSANDVARIABLESUSEDINTHISPROGRAM:
yyerror: Intheeventofsyntaxerrorsyacccallstheusersuppliedfunctionyyerror.

yyparse(): Thisfunctioncallstheyylex()functionwhenitrequirestokensand generatesthesyntaxtree.

yylex(): Toobtaintokensyacccallsyylex.Functionyylexhasareturntypeofint thatreturnsatoken.

yylval: Itisavariablewhichstoresthevalueinthetokens.Itsreturntypeisint.

Internallyyaccmaintainstwostacksinmemory;aparsestackandavaluestack. Theparsestackcontainsterminalsandnonterminalsthatrepresentthecurrent parsingstate.Thevaluestackisanarraythatassociatesavaluewitheachelementin theparsestack.ForexamplewhenlexreturnsanVALUEtokenyaccshiftsthistoken totheparsestack.Atthesametimethecorrespondingyylvalisshiftedtothevalue stack.Theparseandvaluestacksarealwayssynchronizedsofindingavaluerelated toatokenonthestackiseasilyaccomplished.

SIMPLECALCULATOR: Thiscalculatorevaluatessimplearithmeticexpressions.Thelexprogram
matchesnumbersandoperatorsandreturnsthem;itignoreswhitespace,returns newlines,andgivesanerrormessageonanythingelse.

ALGORITHMFORSIMPLECALCULATOR:
1. Inthedefinitionsection,firstdeclarethevariablesfortokenVALUE,andalso theleftandrightassociativerulesasfollows: left'+''', left'*''/''%', rightUMINUS. '+'and''havelowpriorityorprecedenceinanexpressionascomparedto'*','/', and'%'. 2. Inrulesection,formnumberofrulesaccordingtoprogramrequirements a. for+,$1+$3 b. for,$1$3 c. for*,$1*$3

d. for/,$1/$3 e. foruminus,$2 f. forthetermsinbetweenthe'('')',$$=$2 g. Notethat$2storestheresultofunaryexpressions. 3. Initially,themain()functioncallstheprintfstatementwhichallowsuserto enteranexpressionontheconsole. 4. Thisleadstoacalltoyyparse()whichfurthercallsyylex(). 5. Withthehelpofyylex(),getthecharacterstillthespaceisencountered. 6. Nowitwillcheckforcondition,ifitisadigit,performtheoperationandstoreit inthevariableyylvalandreturnthenumberintheformofinteger.

C ONCLUSION: Thus,Wesuccessfullystudiedandimplementedprogramforsimple calculator.

You might also like