SlideShare a Scribd company logo
ProjectCompilers.pdf
Page 1 of 6
Project: Construction of a Simple Parser
INSTRUCTIONS:
You will illustrate the basic phases of the compilation
process (lexical, syntax, and
semantic analysis) through a simple compiler for a programming
language model
“NEWLANG”.
The programming language “NEWLANG” is very simple.
A. Lexical Conventions of NEWLANG
1. The keywords of the language are the following:
declare read write
All keywords are reserved, and must be written in lower case.
2. Special symbols are the following:
{ } ( ) = + - ;
3. Other tokens are NAME that represents a string of letters
and numbers, starting with a
letter, and NUMBER that represents a sequence of digits.
Lower and upper case letters are distinct.
4. White space consists of blanks, newlines, and tabs. White
space is ignored except it
must separate NAME’s, NUMBER’s, and keywords.
Page 2 of 6
B. Syntax and Semantics of NEWLANG
The syntax of “NEWLANG” is described by the grammar rules
defined as follows:
program : { statement_list }
;
statement_list : statement ; statement_list
| ε
;
statement : declaration
| assignment
| read_statement
| write_statement
;
declaration : declare var
;
assignment : var = expression
;
read_statement : read var
;
write_statement : write expression
;
expression : term
| term + term
| term - term
;
term : NUMBER
| var
| ( expression )
;
var : NAME
;
The semantics of ‘NEWLANG” should be clear: a “NEWLANG”
program consists of a
sequence o f read/write or assignment st ate men ts. There are
integer-valued v a r i a b l e s
Page 3 of 6
(which need to be declared before they are used), and
expressions are restricted to
addition and subtraction.
C. Example of NEWLANG source program
A simple NEWLANG program is shown below:
f
{
declare xyz;
xyz = (33+3)-35;
write xyz;
}
The output of the above program is, of course, 1.
D. Project Implementation
The project consists of three phases:
Phase I: Lexical Analysis
With the aid of the lexical analysis generator tool Flex, you will
construct the lexical
analyzer, in order to transform a sequence of input characters
into a sequence of tokens.
Phase II: Syntax Analysis
With the aid of the syntax analysis generator tool Bison, you
will construct the syntax
analyzer, the parser, in order to check whether the sequence of
tokens is grammatically
correct, according to the grammar rules that define the syntax of
the source language.
Looking at the grammar rules for “NEWLANG” (see section B,
above) it seems clear
that a program is syntactically correct if the structure of the
tokens matches the structure
of a <program> as defined by these rules.
Phase III: Semantic Analysis
Having established that the source text is syntactically correct,
the compiler must now
perform additional checks such as determining the type of
expressions and checking that
Page 4 of 6
all statements are correct with respect to the typing rules,
that variables have been
properly declared before they are used, etc.
This phase is carried out using information from the parse tree
and the symbol table.
In our project, very little needs to be checked, due to the
extreme simplicity of the
language. The only checks that are performed verify that a
variable has been declared
before it is used, and whether a variable has been re-declared.
E. Project Implementation Hints
Some important notes follow to help you in the implementation
of the project.
1. In the Yacc/Bison specification, you need to specify the
types of the attributes the
grammar symbols can hold. For example:
%union {
char *str;
int val;
}
. . .
%type <str> NAME var
%type <val> NUMBER
. . .
2. For the symbol table implementation, you need to define a
data structure (e.g., NODE)
with the appropriate members. For example, you may need to
have a member of type
string to hold the name of the identifier, a member of type
integer to hold the
kind of the identifier (e.g., READ, WRITE, or NAME),
and a member of type
integer to mark a symbol in the table as declared.
Further, you may declare an array of type NODE, with a
maximum size, say 100, or
you may create a dynamic linked list (the latter is
considered a more effective
solution than the former one).
Page 5 of 6
Also, for the symbol table management, you need to define two
important operations:
The insert and lookup (find) operations.
The insert function will create a new entry in the symbol table,
whenever a new
identifier is declared.
The lookup (find) operation will search for a specific identifier
in the symbol
table and return whether it has found it or not.
Moreover, you may need to insert the keywords read, write, and
declare in the
symbol table, before the parsing begins (before the call of the
yyparse() function
in the main()), in order not to be used as normal variables
(identifiers).
F. Error Messages
A simple “NEWLANG” program with errors is shown below:
f
{
read x;
x = x+2; y
= x+3;
write y;
declare z;
z = 3- 2;
declare z;
}
When you run your compiler, the following messages should
appear:
$ ./out test-errors.nl
error no 1: Variable "x" not declared (line 2)
error no 2: Variable "x" not declared (line 3)
error no 3: Variable "x" not declared (line 3)
error no 4: Variable "x" not declared (line 4)
error no 5: Variable "y" not declared (line 4)
error no 6: Variable "y" not declared (line 5)
error no 7: Variable "z" already declared (line 8)
README.txt
> bison -vd project.y
> flex project.fl
> gcc -o out project.tab.c lex.yy.c -lfl
> ./out myprog.nl
test.nl
{
declare a;
declare b;
declare c;
declare d;
read a;
read b;
c = (a+3)-2;
d=c;
write (b+d);
}
test-errors.nl
{
read x;
x = x+2;
y = x+3;
write y;
declare z;
z = 3- 2;
declare z;
}
ProjectCompilers.pdfPage 1 of 6 Project Con.docx

More Related Content

PPTX
unit 1 cpds.pptx
PPT
Lecture 01 2017
PPTX
module 4.pptx
PDF
Compiler Design lab manual for Computer Engineering .pdf
PDF
c_programming.pdf
DOC
Pcd question bank
PDF
Handout#02
DOCX
C programming languag for cse students
unit 1 cpds.pptx
Lecture 01 2017
module 4.pptx
Compiler Design lab manual for Computer Engineering .pdf
c_programming.pdf
Pcd question bank
Handout#02
C programming languag for cse students

Similar to ProjectCompilers.pdfPage 1 of 6 Project Con.docx (20)

DOCX
C notes
PPTX
Compiler Design Notes for rgpv 6tth sem students
PDF
data base nd analystics slybysss for the students to improbvr
PPTX
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
PPTX
comp 122 Chapter 2.pptx,language semantics
PPTX
Aniket tore
PDF
LANGUAGE PROCESSOR
PPT
LEX lexical analyzer for compiler theory.ppt
DOCX
Cs6660 compiler design may june 2016 Answer Key
PPTX
Chapter 2.pptx compiler design lecture note
PPT
Symbol Table, Error Handler & Code Generation
PDF
1588147798Begining_ABUAD1.pdf
PPTX
Plc part 2
PDF
match the following attributes to the parts of a compilerstrips ou.pdf
DOCX
Compiler design important questions
DOC
DOCX
Compiler Design
PPTX
programming for problem solving in C and C++.pptx
PDF
Language processors
PDF
C programming course material
C notes
Compiler Design Notes for rgpv 6tth sem students
data base nd analystics slybysss for the students to improbvr
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
comp 122 Chapter 2.pptx,language semantics
Aniket tore
LANGUAGE PROCESSOR
LEX lexical analyzer for compiler theory.ppt
Cs6660 compiler design may june 2016 Answer Key
Chapter 2.pptx compiler design lecture note
Symbol Table, Error Handler & Code Generation
1588147798Begining_ABUAD1.pdf
Plc part 2
match the following attributes to the parts of a compilerstrips ou.pdf
Compiler design important questions
Compiler Design
programming for problem solving in C and C++.pptx
Language processors
C programming course material

More from wkyra78 (20)

DOCX
Melissa HinkhouseWeek 3-Original PostNURS 6050 Policy and A.docx
DOCX
Melissa HinkhouseAdvanced Pharmacology NURS-6521N-43Professo.docx
DOCX
Meiner, S. E., & Yeager, J. J. (2019). Chapter 17Chap.docx
DOCX
member is a security software architect in a cloud service provider .docx
DOCX
Melissa ShortridgeWeek 6COLLAPSEMy own attitude has ch.docx
DOCX
Melissa is a 15-year-old high school student. Over the last week.docx
DOCX
Measurement  of  the  angle  θ          .docx
DOCX
Measurement of the angle θ For better understanding .docx
DOCX
Meaning-Making Forum 2 (Week 5)Meaning-Making Forums 1-4 are thi.docx
DOCX
MBA6231 - 1.1 - project charter.docxProject Charter Pr.docx
DOCX
Medication Errors Led to Disastrous Outcomes1. Search th.docx
DOCX
Meet, call, Skype or Zoom with a retired athlete and interview himh.docx
DOCX
Medication Administration Make a list of the most common med.docx
DOCX
media portfolio”about chapter 1 to 15 from the book  Ci.docx
DOCX
MediationNameAMUDate.docx
DOCX
Media coverage influences the publics perception of the crimina.docx
DOCX
Media Content AnalysisPurpose Evaluate the quality and value of.docx
DOCX
Mayan gods and goddesses are very much a part of this text.  Their i.docx
DOCX
Media and SocietyIn 1,100 words, complete the followingAn.docx
DOCX
MBA 5110 – Business Organization and ManagementMidterm ExamAns.docx
Melissa HinkhouseWeek 3-Original PostNURS 6050 Policy and A.docx
Melissa HinkhouseAdvanced Pharmacology NURS-6521N-43Professo.docx
Meiner, S. E., & Yeager, J. J. (2019). Chapter 17Chap.docx
member is a security software architect in a cloud service provider .docx
Melissa ShortridgeWeek 6COLLAPSEMy own attitude has ch.docx
Melissa is a 15-year-old high school student. Over the last week.docx
Measurement  of  the  angle  θ          .docx
Measurement of the angle θ For better understanding .docx
Meaning-Making Forum 2 (Week 5)Meaning-Making Forums 1-4 are thi.docx
MBA6231 - 1.1 - project charter.docxProject Charter Pr.docx
Medication Errors Led to Disastrous Outcomes1. Search th.docx
Meet, call, Skype or Zoom with a retired athlete and interview himh.docx
Medication Administration Make a list of the most common med.docx
media portfolio”about chapter 1 to 15 from the book  Ci.docx
MediationNameAMUDate.docx
Media coverage influences the publics perception of the crimina.docx
Media Content AnalysisPurpose Evaluate the quality and value of.docx
Mayan gods and goddesses are very much a part of this text.  Their i.docx
Media and SocietyIn 1,100 words, complete the followingAn.docx
MBA 5110 – Business Organization and ManagementMidterm ExamAns.docx

Recently uploaded (20)

PPTX
Cell Structure & Organelles in detailed.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
PDF
Computing-Curriculum for Schools in Ghana
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
Lesson notes of climatology university.
PDF
Trump Administration's workforce development strategy
PDF
Classroom Observation Tools for Teachers
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Cell Structure & Organelles in detailed.
Final Presentation General Medicine 03-08-2024.pptx
Orientation - ARALprogram of Deped to the Parents.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Computing-Curriculum for Schools in Ghana
2.FourierTransform-ShortQuestionswithAnswers.pdf
What if we spent less time fighting change, and more time building what’s rig...
LNK 2025 (2).pdf MWEHEHEHEHEHEHEHEHEHEHE
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
A systematic review of self-coping strategies used by university students to ...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Lesson notes of climatology university.
Trump Administration's workforce development strategy
Classroom Observation Tools for Teachers
Final Presentation General Medicine 03-08-2024.pptx

ProjectCompilers.pdfPage 1 of 6 Project Con.docx

  • 1. ProjectCompilers.pdf Page 1 of 6 Project: Construction of a Simple Parser INSTRUCTIONS: You will illustrate the basic phases of the compilation process (lexical, syntax, and semantic analysis) through a simple compiler for a programming language model “NEWLANG”. The programming language “NEWLANG” is very simple. A. Lexical Conventions of NEWLANG 1. The keywords of the language are the following:
  • 2. declare read write All keywords are reserved, and must be written in lower case. 2. Special symbols are the following: { } ( ) = + - ; 3. Other tokens are NAME that represents a string of letters and numbers, starting with a letter, and NUMBER that represents a sequence of digits. Lower and upper case letters are distinct. 4. White space consists of blanks, newlines, and tabs. White space is ignored except it must separate NAME’s, NUMBER’s, and keywords. Page 2 of 6 B. Syntax and Semantics of NEWLANG
  • 3. The syntax of “NEWLANG” is described by the grammar rules defined as follows: program : { statement_list } ; statement_list : statement ; statement_list | ε ; statement : declaration | assignment | read_statement | write_statement ; declaration : declare var ; assignment : var = expression ; read_statement : read var ; write_statement : write expression ;
  • 4. expression : term | term + term | term - term ; term : NUMBER | var | ( expression ) ; var : NAME ; The semantics of ‘NEWLANG” should be clear: a “NEWLANG” program consists of a sequence o f read/write or assignment st ate men ts. There are integer-valued v a r i a b l e s Page 3 of 6 (which need to be declared before they are used), and expressions are restricted to addition and subtraction.
  • 5. C. Example of NEWLANG source program A simple NEWLANG program is shown below: f { declare xyz; xyz = (33+3)-35; write xyz; } The output of the above program is, of course, 1. D. Project Implementation The project consists of three phases: Phase I: Lexical Analysis With the aid of the lexical analysis generator tool Flex, you will construct the lexical analyzer, in order to transform a sequence of input characters into a sequence of tokens. Phase II: Syntax Analysis
  • 6. With the aid of the syntax analysis generator tool Bison, you will construct the syntax analyzer, the parser, in order to check whether the sequence of tokens is grammatically correct, according to the grammar rules that define the syntax of the source language. Looking at the grammar rules for “NEWLANG” (see section B, above) it seems clear that a program is syntactically correct if the structure of the tokens matches the structure of a <program> as defined by these rules. Phase III: Semantic Analysis Having established that the source text is syntactically correct, the compiler must now perform additional checks such as determining the type of expressions and checking that Page 4 of 6
  • 7. all statements are correct with respect to the typing rules, that variables have been properly declared before they are used, etc. This phase is carried out using information from the parse tree and the symbol table. In our project, very little needs to be checked, due to the extreme simplicity of the language. The only checks that are performed verify that a variable has been declared before it is used, and whether a variable has been re-declared. E. Project Implementation Hints Some important notes follow to help you in the implementation of the project. 1. In the Yacc/Bison specification, you need to specify the types of the attributes the grammar symbols can hold. For example: %union {
  • 8. char *str; int val; } . . . %type <str> NAME var %type <val> NUMBER . . . 2. For the symbol table implementation, you need to define a data structure (e.g., NODE) with the appropriate members. For example, you may need to have a member of type string to hold the name of the identifier, a member of type integer to hold the kind of the identifier (e.g., READ, WRITE, or NAME), and a member of type integer to mark a symbol in the table as declared. Further, you may declare an array of type NODE, with a maximum size, say 100, or you may create a dynamic linked list (the latter is considered a more effective
  • 9. solution than the former one). Page 5 of 6 Also, for the symbol table management, you need to define two important operations: The insert and lookup (find) operations. The insert function will create a new entry in the symbol table, whenever a new identifier is declared. The lookup (find) operation will search for a specific identifier in the symbol table and return whether it has found it or not. Moreover, you may need to insert the keywords read, write, and declare in the symbol table, before the parsing begins (before the call of the yyparse() function in the main()), in order not to be used as normal variables (identifiers).
  • 10. F. Error Messages A simple “NEWLANG” program with errors is shown below: f { read x; x = x+2; y = x+3; write y; declare z; z = 3- 2; declare z; } When you run your compiler, the following messages should appear: $ ./out test-errors.nl error no 1: Variable "x" not declared (line 2) error no 2: Variable "x" not declared (line 3)
  • 11. error no 3: Variable "x" not declared (line 3) error no 4: Variable "x" not declared (line 4) error no 5: Variable "y" not declared (line 4) error no 6: Variable "y" not declared (line 5) error no 7: Variable "z" already declared (line 8) README.txt > bison -vd project.y > flex project.fl > gcc -o out project.tab.c lex.yy.c -lfl > ./out myprog.nl test.nl { declare a; declare b; declare c;
  • 12. declare d; read a; read b; c = (a+3)-2; d=c; write (b+d); } test-errors.nl { read x; x = x+2; y = x+3; write y; declare z; z = 3- 2; declare z; }