SlideShare a Scribd company logo
1 of 15
Home work for modifying the syntactic analyzer for the
attached compiler by adding to the existing grammar. The full
grammar of the language is shown below. The highlighted
portions of the grammar show what you must either modify or
add to the existing grammar.
function:
function_header {variable} body
function_header:
FUNCTION IDENTIFIER [parameters] RETURNS type ;
variable:
IDENTIFIER : type IS statement
parameters:
parameter {, parameter}
parameter:
IDENTIFIER : type
type:
INTEGER | REAL | BOOLEAN
body:
BEGIN statement END ;
statement: expression ; |
REDUCE operator {statement} ENDREDUCE ; |
IF expression THEN statement ELSE statement ENDIF ; |
CASE expression IS {case} OTHERS ARROW statement ;
ENDCASE ;
operator:
ADDOP | MULOP
case:
WHEN INT_LITERAL ARROW statement
expression:
( expression ) |
REAL_LITERAL
NOT expression
expression binary_operator expression |
|
INT_LITERAL | IDENTIFIER
| BOOL_LITERAL |
binary_operator: ADDOP | MULOP | REMOP | EXPOP | RELOP
| ANDOP | OROP
In the above grammar, the red symbols are nonterminals, the
blue symbols are terminals and the black punctuation are EBNF
metasymbols. The braces denote repetition 0 or more times and
the brackets denote optional.
You must rewrite the grammar to eliminate the EBNF brace and
bracket metasymbols and to incorporate the significance of
parentheses, operator precedence and associativity for all
operators. Among arithmetic operators the exponentiation
operator has highest precedence following by the multiplying
operators and then the adding operators. All relational operators
have the same precedence. Among the binary logical operators,
and has higher precedence than or. Of the categories of
operators, the unary logical operator has highest precedence, the
arithmetic operators have next highest precedence, followed by
the relational operators and finally the binary logical operators.
All operators except the exponentiation operator are left
associative. The directives to specify precedence and
associativity, such as %prec and %left, may not be used
Your parser should be able to correctly parse any syntactically
correct program without any problem.
You must modify the syntactic analyzer to detect and recover
from additional syntax errors using the semicolon as the
synchronization token. To accomplish detecting additional
errors an error production must be added to the function header
and another to the variable declaration.
Your bison input file should not produce any shift/reduce or
reduce/reduce errors. Eliminating them can be difficult so the
best strategy is not introduce any. That is best achieved by
making small incremental additions to the grammar and
ensuring that no addition introduces any such errors.
An example of compilation listing output containing syntax
errors is shown below:
1 -- Multiple errors 2
function main a integer returns real; Syntax Error, Unexpected
INTEGER, expecting ':'
b: integer is * 2; Syntax Error, Unexpected MULOP
c: real is 6.0;
begin
if a > c then 8 b 3.0;
Syntax Error, Unexpected REAL_LITERAL, expecting ';'
9 else
10 b = 4.;
11 endif;
12 ;
Syntax Error, Unexpected ';', expecting END
Lexical Errors 0
Syntax Errors 4
Semantic Errors 0
------------------------------------------------------------
listing.h
// This file contains the function prototypes for the functions
that produce the // compilation listing
enum ErrorCategories {LEXICAL, SYNTAX,
GENERAL_SEMANTIC, DUPLICATE_IDENTIFIER,
UNDECLARED};
void firstLine();
void nextLine();
int lastLine();
void appendError(ErrorCategories errorCategory, string
message);
---------------------------------------------------------------------------
-------
makefile
compile: scanner.o parser.o listing.o
g++ -o compile scanner.o parser.o listing.o
scanner.o: scanner.c listing.h tokens.h
g++ -c scanner.c
scanner.c: scanner.l
flex scanner.l
mv lex.yy.c scanner.c
parser.o: parser.c listing.h
g++ -c parser.c
parser.c tokens.h: parser.y
bison -d -v parser.y
mv parser.tab.c parser.c
mv parser.tab.h tokens.h
listing.o: listing.cc listing.h
g++ -c listing.cc
----------------------------------------------------------
parser.y
%{
#include
using namespace std;
#include "listing.h"
int yylex();
void yyerror(const char* message);
%}
%error-verbose
%token IDENTIFIER
%token INT_LITERAL
%token ADDOP MULOP RELOP ANDOP
%token BEGIN_ BOOLEAN END ENDREDUCE FUNCTION
INTEGER IS REDUCE RETURNS
%%
function:
function_header optional_variable body ;
function_header:
FUNCTION IDENTIFIER RETURNS type ';' ;
optional_variable:
variable |
;
variable:
IDENTIFIER ':' type IS statement_ ;
type:
INTEGER |
BOOLEAN ;
body:
BEGIN_ statement_ END ';' ;
statement_:
statement ';' |
error ';' ;
statement:
expression |
REDUCE operator reductions ENDREDUCE ;
operator:
ADDOP |
MULOP ;
reductions:
reductions statement_ |
;
expression:
expression ANDOP relation |
relation ;
relation:
relation RELOP term |
term;
term:
term ADDOP factor |
factor ;
factor:
factor MULOP primary |
primary ;
primary:
'(' expression ')' |
INT_LITERAL |
IDENTIFIER ;
%%
void yyerror(const char* message)
{
appendError(SYNTAX, message);
}
int main(int argc, char *argv[])
{
firstLine();
yyparse();
lastLine();
return 0;
}
------------------------------------------------------------------------
scanner
/* Compiler Theory and Design
Dr. Duane J. Jarc */
/* This file contains flex input file */
%{
#include
#include
using namespace std;
#include "listing.h"
#include "tokens.h"
%}
%option noyywrap
ws [ tr]+
comment --.*n
line [n]
id [A-Za-z][A-Za-z0-9]*
digit [0-9]
int {digit}+
punc [(),:;]
%%
{ws} { ECHO; }
{comment} { ECHO; nextLine();}
{line} { ECHO; nextLine();}
"<" { ECHO; return(RELOP); }
"+" { ECHO; return(ADDOP); }
"*" { ECHO; return(MULOP); }
begin { ECHO; return(BEGIN_); }
boolean { ECHO; return(BOOLEAN); }
end { ECHO; return(END); }
endreduce { ECHO; return(ENDREDUCE); }
function { ECHO; return(FUNCTION); }
integer { ECHO; return(INTEGER); }
is { ECHO; return(IS); }
reduce { ECHO; return REDUCE; }
returns { ECHO; return(RETURNS); }
and { ECHO; return(ANDOP); }
{id} { ECHO; return(IDENTIFIER);}
{int} { ECHO; return(INT_LITERAL); }
{punc} { ECHO; return(yytext[0]); }
. { ECHO; appendError(LEXICAL, yytext); }
%%
----------------------------------------------------------------
listing.cc
// Compiler Theory and Design
// Dr. Duane J. Jarc
// This file contains the bodies of the functions that produces
the compilation
// listing
#include
#include
using namespace std;
#include "listing.h"
static int lineNumber;
static string error = "";
static int totalErrors = 0;
static void displayErrors();
void firstLine()
{
lineNumber = 1;
printf("n%4d ",lineNumber);
}
void nextLine()
{
displayErrors();
lineNumber++;
printf("%4d ",lineNumber);
}
int lastLine()
{
printf("r");
displayErrors();
printf(" n");
return totalErrors;
}
void appendError(ErrorCategories errorCategory, string
message)
{
string messages[] = { "Lexical Error, Invalid Character ", "",
"Semantic Error, ", "Semantic Error, Duplicate Identifier:
",
"Semantic Error, Undeclared " };
error = messages[errorCategory] + message;
totalErrors++;
}
void displayErrors()
{
if (error != "")
printf("%sn", error.c_str());
error = "";
}
----------------------------------------------------------

More Related Content

Similar to Home work for modifying the syntactic analyzer for the attached comp.docx

CS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docxCS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docxrichardnorman90310
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdfziyadaslanbey
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in cfloraaluoch3
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handlingmussawir20
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kianphelios
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 TrainingChris Chubb
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdfHome
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuationlotlot
 
C programming session 02
C programming session 02C programming session 02
C programming session 02Dushmanta Nath
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxKrishanPalSingh39
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Thuan Nguyen
 

Similar to Home work for modifying the syntactic analyzer for the attached comp.docx (20)

CS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docxCS 280 Fall 2022 Programming Assignment 2 November.docx
CS 280 Fall 2022 Programming Assignment 2 November.docx
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
Functions.pptx, programming language in c
Functions.pptx, programming language in cFunctions.pptx, programming language in c
Functions.pptx, programming language in c
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handling
 
P H P Part I, By Kian
P H P  Part  I,  By  KianP H P  Part  I,  By  Kian
P H P Part I, By Kian
 
Php
PhpPhp
Php
 
Php Chapter 1 Training
Php Chapter 1 TrainingPhp Chapter 1 Training
Php Chapter 1 Training
 
Functions
FunctionsFunctions
Functions
 
CProgrammingTutorial
CProgrammingTutorialCProgrammingTutorial
CProgrammingTutorial
 
Php basics
Php basicsPhp basics
Php basics
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
 
Php-Continuation
Php-ContinuationPhp-Continuation
Php-Continuation
 
Ch02
Ch02Ch02
Ch02
 
C++ programming
C++ programmingC++ programming
C++ programming
 
C programming session 02
C programming session 02C programming session 02
C programming session 02
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
C function presentation
C function presentationC function presentation
C function presentation
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09Oracle - Program with PL/SQL - Lession 09
Oracle - Program with PL/SQL - Lession 09
 

More from simonithomas47935

Hours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docx
Hours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docxHours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docx
Hours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docxsimonithomas47935
 
How are authentication and authorization alike and how are the.docx
How are authentication and authorization alike and how are the.docxHow are authentication and authorization alike and how are the.docx
How are authentication and authorization alike and how are the.docxsimonithomas47935
 
How are self-esteem and self-concept different What is the or.docx
How are self-esteem and self-concept different What is the or.docxHow are self-esteem and self-concept different What is the or.docx
How are self-esteem and self-concept different What is the or.docxsimonithomas47935
 
How are morality and religion similar and how are they different.docx
How are morality and religion similar and how are they different.docxHow are morality and religion similar and how are they different.docx
How are morality and religion similar and how are they different.docxsimonithomas47935
 
How are financial statements used to evaluate business activities.docx
How are financial statements used to evaluate business activities.docxHow are financial statements used to evaluate business activities.docx
How are financial statements used to evaluate business activities.docxsimonithomas47935
 
How are Japanese and Chinese Americans similar How are they differe.docx
How are Japanese and Chinese Americans similar How are they differe.docxHow are Japanese and Chinese Americans similar How are they differe.docx
How are Japanese and Chinese Americans similar How are they differe.docxsimonithomas47935
 
Hot Spot PolicingPlace can be an important aspect of crime and.docx
Hot Spot PolicingPlace can be an important aspect of crime and.docxHot Spot PolicingPlace can be an important aspect of crime and.docx
Hot Spot PolicingPlace can be an important aspect of crime and.docxsimonithomas47935
 
HOSP3075 Brand Analysis Paper 1This is the first of three assignme.docx
HOSP3075 Brand Analysis Paper 1This is the first of three assignme.docxHOSP3075 Brand Analysis Paper 1This is the first of three assignme.docx
HOSP3075 Brand Analysis Paper 1This is the first of three assignme.docxsimonithomas47935
 
Hou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docx
Hou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docxHou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docx
Hou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docxsimonithomas47935
 
How (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docx
How (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docxHow (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docx
How (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docxsimonithomas47935
 
Hopefully, you enjoyed this class on Digital Media and Society.Q.docx
Hopefully, you enjoyed this class on Digital Media and Society.Q.docxHopefully, you enjoyed this class on Digital Media and Society.Q.docx
Hopefully, you enjoyed this class on Digital Media and Society.Q.docxsimonithomas47935
 
hoose (1) one childhood experience from the list provided below..docx
hoose (1) one childhood experience from the list provided below..docxhoose (1) one childhood experience from the list provided below..docx
hoose (1) one childhood experience from the list provided below..docxsimonithomas47935
 
honesty, hard work, caring, excellence HIS 1110 Dr. .docx
honesty, hard work, caring, excellence  HIS 1110      Dr. .docxhonesty, hard work, caring, excellence  HIS 1110      Dr. .docx
honesty, hard work, caring, excellence HIS 1110 Dr. .docxsimonithomas47935
 
hoose one of the four following visualsImage courtesy o.docx
hoose one of the four following visualsImage courtesy o.docxhoose one of the four following visualsImage courtesy o.docx
hoose one of the four following visualsImage courtesy o.docxsimonithomas47935
 
HomeworkChoose a site used by the public such as a supermark.docx
HomeworkChoose a site used by the public such as a supermark.docxHomeworkChoose a site used by the public such as a supermark.docx
HomeworkChoose a site used by the public such as a supermark.docxsimonithomas47935
 
Homework 2 Please answer the following questions in small paragraph.docx
Homework 2 Please answer the following questions in small paragraph.docxHomework 2 Please answer the following questions in small paragraph.docx
Homework 2 Please answer the following questions in small paragraph.docxsimonithomas47935
 
HomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docx
HomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docxHomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docx
HomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docxsimonithomas47935
 
HomeAnnouncementsSyllabusDiscussionsQuizzesGra.docx
HomeAnnouncementsSyllabusDiscussionsQuizzesGra.docxHomeAnnouncementsSyllabusDiscussionsQuizzesGra.docx
HomeAnnouncementsSyllabusDiscussionsQuizzesGra.docxsimonithomas47935
 
Homeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docx
Homeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docxHomeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docx
Homeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docxsimonithomas47935
 
Home work 8 Date 042220201. what are the different between.docx
Home work  8 Date 042220201. what are the  different between.docxHome work  8 Date 042220201. what are the  different between.docx
Home work 8 Date 042220201. what are the different between.docxsimonithomas47935
 

More from simonithomas47935 (20)

Hours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docx
Hours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docxHours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docx
Hours, A. (2014). Reading Fairy Tales and Playing A Way of Treati.docx
 
How are authentication and authorization alike and how are the.docx
How are authentication and authorization alike and how are the.docxHow are authentication and authorization alike and how are the.docx
How are authentication and authorization alike and how are the.docx
 
How are self-esteem and self-concept different What is the or.docx
How are self-esteem and self-concept different What is the or.docxHow are self-esteem and self-concept different What is the or.docx
How are self-esteem and self-concept different What is the or.docx
 
How are morality and religion similar and how are they different.docx
How are morality and religion similar and how are they different.docxHow are morality and religion similar and how are they different.docx
How are morality and religion similar and how are they different.docx
 
How are financial statements used to evaluate business activities.docx
How are financial statements used to evaluate business activities.docxHow are financial statements used to evaluate business activities.docx
How are financial statements used to evaluate business activities.docx
 
How are Japanese and Chinese Americans similar How are they differe.docx
How are Japanese and Chinese Americans similar How are they differe.docxHow are Japanese and Chinese Americans similar How are they differe.docx
How are Japanese and Chinese Americans similar How are they differe.docx
 
Hot Spot PolicingPlace can be an important aspect of crime and.docx
Hot Spot PolicingPlace can be an important aspect of crime and.docxHot Spot PolicingPlace can be an important aspect of crime and.docx
Hot Spot PolicingPlace can be an important aspect of crime and.docx
 
HOSP3075 Brand Analysis Paper 1This is the first of three assignme.docx
HOSP3075 Brand Analysis Paper 1This is the first of three assignme.docxHOSP3075 Brand Analysis Paper 1This is the first of three assignme.docx
HOSP3075 Brand Analysis Paper 1This is the first of three assignme.docx
 
Hou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docx
Hou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docxHou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docx
Hou, J., Li, Y., Yu, J. & Shi, W. (2020). A Survey on Digital Fo.docx
 
How (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docx
How (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docxHow (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docx
How (Not) to be Secular by James K.A. SmithSecular (1)—the ea.docx
 
Hopefully, you enjoyed this class on Digital Media and Society.Q.docx
Hopefully, you enjoyed this class on Digital Media and Society.Q.docxHopefully, you enjoyed this class on Digital Media and Society.Q.docx
Hopefully, you enjoyed this class on Digital Media and Society.Q.docx
 
hoose (1) one childhood experience from the list provided below..docx
hoose (1) one childhood experience from the list provided below..docxhoose (1) one childhood experience from the list provided below..docx
hoose (1) one childhood experience from the list provided below..docx
 
honesty, hard work, caring, excellence HIS 1110 Dr. .docx
honesty, hard work, caring, excellence  HIS 1110      Dr. .docxhonesty, hard work, caring, excellence  HIS 1110      Dr. .docx
honesty, hard work, caring, excellence HIS 1110 Dr. .docx
 
hoose one of the four following visualsImage courtesy o.docx
hoose one of the four following visualsImage courtesy o.docxhoose one of the four following visualsImage courtesy o.docx
hoose one of the four following visualsImage courtesy o.docx
 
HomeworkChoose a site used by the public such as a supermark.docx
HomeworkChoose a site used by the public such as a supermark.docxHomeworkChoose a site used by the public such as a supermark.docx
HomeworkChoose a site used by the public such as a supermark.docx
 
Homework 2 Please answer the following questions in small paragraph.docx
Homework 2 Please answer the following questions in small paragraph.docxHomework 2 Please answer the following questions in small paragraph.docx
Homework 2 Please answer the following questions in small paragraph.docx
 
HomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docx
HomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docxHomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docx
HomeNotificationsMy CommunityBBA 2010-16J-5A21-S1, Introductio.docx
 
HomeAnnouncementsSyllabusDiscussionsQuizzesGra.docx
HomeAnnouncementsSyllabusDiscussionsQuizzesGra.docxHomeAnnouncementsSyllabusDiscussionsQuizzesGra.docx
HomeAnnouncementsSyllabusDiscussionsQuizzesGra.docx
 
Homeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docx
Homeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docxHomeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docx
Homeless The Motel Kids of Orange CountyWrite a 1-2 page pa.docx
 
Home work 8 Date 042220201. what are the different between.docx
Home work  8 Date 042220201. what are the  different between.docxHome work  8 Date 042220201. what are the  different between.docx
Home work 8 Date 042220201. what are the different between.docx
 

Recently uploaded

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 

Recently uploaded (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 

Home work for modifying the syntactic analyzer for the attached comp.docx

  • 1. Home work for modifying the syntactic analyzer for the attached compiler by adding to the existing grammar. The full grammar of the language is shown below. The highlighted portions of the grammar show what you must either modify or add to the existing grammar. function: function_header {variable} body function_header: FUNCTION IDENTIFIER [parameters] RETURNS type ; variable: IDENTIFIER : type IS statement parameters: parameter {, parameter} parameter: IDENTIFIER : type type: INTEGER | REAL | BOOLEAN body: BEGIN statement END ; statement: expression ; |
  • 2. REDUCE operator {statement} ENDREDUCE ; | IF expression THEN statement ELSE statement ENDIF ; | CASE expression IS {case} OTHERS ARROW statement ; ENDCASE ; operator: ADDOP | MULOP case: WHEN INT_LITERAL ARROW statement expression: ( expression ) | REAL_LITERAL NOT expression expression binary_operator expression | | INT_LITERAL | IDENTIFIER | BOOL_LITERAL | binary_operator: ADDOP | MULOP | REMOP | EXPOP | RELOP | ANDOP | OROP In the above grammar, the red symbols are nonterminals, the
  • 3. blue symbols are terminals and the black punctuation are EBNF metasymbols. The braces denote repetition 0 or more times and the brackets denote optional. You must rewrite the grammar to eliminate the EBNF brace and bracket metasymbols and to incorporate the significance of parentheses, operator precedence and associativity for all operators. Among arithmetic operators the exponentiation operator has highest precedence following by the multiplying operators and then the adding operators. All relational operators have the same precedence. Among the binary logical operators, and has higher precedence than or. Of the categories of operators, the unary logical operator has highest precedence, the arithmetic operators have next highest precedence, followed by the relational operators and finally the binary logical operators. All operators except the exponentiation operator are left associative. The directives to specify precedence and associativity, such as %prec and %left, may not be used Your parser should be able to correctly parse any syntactically correct program without any problem. You must modify the syntactic analyzer to detect and recover from additional syntax errors using the semicolon as the synchronization token. To accomplish detecting additional errors an error production must be added to the function header and another to the variable declaration. Your bison input file should not produce any shift/reduce or reduce/reduce errors. Eliminating them can be difficult so the best strategy is not introduce any. That is best achieved by making small incremental additions to the grammar and ensuring that no addition introduces any such errors. An example of compilation listing output containing syntax errors is shown below:
  • 4. 1 -- Multiple errors 2 function main a integer returns real; Syntax Error, Unexpected INTEGER, expecting ':' b: integer is * 2; Syntax Error, Unexpected MULOP c: real is 6.0; begin if a > c then 8 b 3.0; Syntax Error, Unexpected REAL_LITERAL, expecting ';' 9 else 10 b = 4.; 11 endif; 12 ; Syntax Error, Unexpected ';', expecting END Lexical Errors 0 Syntax Errors 4 Semantic Errors 0 ------------------------------------------------------------
  • 5. listing.h // This file contains the function prototypes for the functions that produce the // compilation listing enum ErrorCategories {LEXICAL, SYNTAX, GENERAL_SEMANTIC, DUPLICATE_IDENTIFIER, UNDECLARED}; void firstLine(); void nextLine(); int lastLine(); void appendError(ErrorCategories errorCategory, string message); --------------------------------------------------------------------------- ------- makefile compile: scanner.o parser.o listing.o g++ -o compile scanner.o parser.o listing.o scanner.o: scanner.c listing.h tokens.h g++ -c scanner.c scanner.c: scanner.l flex scanner.l
  • 6. mv lex.yy.c scanner.c parser.o: parser.c listing.h g++ -c parser.c parser.c tokens.h: parser.y bison -d -v parser.y mv parser.tab.c parser.c mv parser.tab.h tokens.h listing.o: listing.cc listing.h g++ -c listing.cc ---------------------------------------------------------- parser.y %{ #include using namespace std; #include "listing.h" int yylex(); void yyerror(const char* message); %}
  • 7. %error-verbose %token IDENTIFIER %token INT_LITERAL %token ADDOP MULOP RELOP ANDOP %token BEGIN_ BOOLEAN END ENDREDUCE FUNCTION INTEGER IS REDUCE RETURNS %% function: function_header optional_variable body ; function_header: FUNCTION IDENTIFIER RETURNS type ';' ; optional_variable: variable | ; variable: IDENTIFIER ':' type IS statement_ ; type: INTEGER |
  • 8. BOOLEAN ; body: BEGIN_ statement_ END ';' ; statement_: statement ';' | error ';' ; statement: expression | REDUCE operator reductions ENDREDUCE ; operator: ADDOP | MULOP ; reductions: reductions statement_ | ; expression:
  • 9. expression ANDOP relation | relation ; relation: relation RELOP term | term; term: term ADDOP factor | factor ; factor: factor MULOP primary | primary ; primary: '(' expression ')' | INT_LITERAL | IDENTIFIER ; %% void yyerror(const char* message)
  • 10. { appendError(SYNTAX, message); } int main(int argc, char *argv[]) { firstLine(); yyparse(); lastLine(); return 0; } ------------------------------------------------------------------------ scanner /* Compiler Theory and Design Dr. Duane J. Jarc */ /* This file contains flex input file */ %{ #include #include
  • 11. using namespace std; #include "listing.h" #include "tokens.h" %} %option noyywrap ws [ tr]+ comment --.*n line [n] id [A-Za-z][A-Za-z0-9]* digit [0-9] int {digit}+ punc [(),:;] %% {ws} { ECHO; } {comment} { ECHO; nextLine();} {line} { ECHO; nextLine();} "<" { ECHO; return(RELOP); } "+" { ECHO; return(ADDOP); }
  • 12. "*" { ECHO; return(MULOP); } begin { ECHO; return(BEGIN_); } boolean { ECHO; return(BOOLEAN); } end { ECHO; return(END); } endreduce { ECHO; return(ENDREDUCE); } function { ECHO; return(FUNCTION); } integer { ECHO; return(INTEGER); } is { ECHO; return(IS); } reduce { ECHO; return REDUCE; } returns { ECHO; return(RETURNS); } and { ECHO; return(ANDOP); } {id} { ECHO; return(IDENTIFIER);} {int} { ECHO; return(INT_LITERAL); } {punc} { ECHO; return(yytext[0]); } . { ECHO; appendError(LEXICAL, yytext); } %% ---------------------------------------------------------------- listing.cc
  • 13. // Compiler Theory and Design // Dr. Duane J. Jarc // This file contains the bodies of the functions that produces the compilation // listing #include #include using namespace std; #include "listing.h" static int lineNumber; static string error = ""; static int totalErrors = 0; static void displayErrors(); void firstLine() { lineNumber = 1; printf("n%4d ",lineNumber); } void nextLine()
  • 14. { displayErrors(); lineNumber++; printf("%4d ",lineNumber); } int lastLine() { printf("r"); displayErrors(); printf(" n"); return totalErrors; } void appendError(ErrorCategories errorCategory, string message) { string messages[] = { "Lexical Error, Invalid Character ", "", "Semantic Error, ", "Semantic Error, Duplicate Identifier: ",
  • 15. "Semantic Error, Undeclared " }; error = messages[errorCategory] + message; totalErrors++; } void displayErrors() { if (error != "") printf("%sn", error.c_str()); error = ""; } ----------------------------------------------------------