SlideShare a Scribd company logo
1 of 13
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

Similar to ProjectCompilers.pdfPage 1 of 6 Project Con.docx

match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
arpitaeron555
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
indrasir
 

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

C notes
C notesC notes
C notes
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Aniket tore
Aniket toreAniket tore
Aniket tore
 
LANGUAGE PROCESSOR
LANGUAGE PROCESSORLANGUAGE PROCESSOR
LANGUAGE PROCESSOR
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Symbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code GenerationSymbol Table, Error Handler & Code Generation
Symbol Table, Error Handler & Code Generation
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
 
Compiler design important questions
Compiler design   important questionsCompiler design   important questions
Compiler design important questions
 
Cnotes
CnotesCnotes
Cnotes
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Language processors
Language processorsLanguage processors
Language processors
 
C programming course material
C programming course materialC programming course material
C programming course material
 
Chapter3
Chapter3Chapter3
Chapter3
 
C programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer CentreC programming Training in Ambala ! Batra Computer Centre
C programming Training in Ambala ! Batra Computer Centre
 
Btech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c languageBtech i pic u-2 datatypes and variables in c language
Btech i pic u-2 datatypes and variables in c language
 
Mca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c languageMca i pic u-2 datatypes and variables in c language
Mca i pic u-2 datatypes and variables in c language
 
C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)C basics 4 std11(GujBoard)
C basics 4 std11(GujBoard)
 
C notes.pdf
C notes.pdfC notes.pdf
C notes.pdf
 

More from wkyra78

Melissa HinkhouseWeek 3-Original PostNURS 6050 Policy and A.docx
Melissa HinkhouseWeek 3-Original PostNURS 6050 Policy and A.docxMelissa HinkhouseWeek 3-Original PostNURS 6050 Policy and A.docx
Melissa HinkhouseWeek 3-Original PostNURS 6050 Policy and A.docx
wkyra78
 
Melissa HinkhouseAdvanced Pharmacology NURS-6521N-43Professo.docx
Melissa HinkhouseAdvanced Pharmacology NURS-6521N-43Professo.docxMelissa HinkhouseAdvanced Pharmacology NURS-6521N-43Professo.docx
Melissa HinkhouseAdvanced Pharmacology NURS-6521N-43Professo.docx
wkyra78
 
member is a security software architect in a cloud service provider .docx
member is a security software architect in a cloud service provider .docxmember is a security software architect in a cloud service provider .docx
member is a security software architect in a cloud service provider .docx
wkyra78
 
Melissa ShortridgeWeek 6COLLAPSEMy own attitude has ch.docx
Melissa ShortridgeWeek 6COLLAPSEMy own attitude has ch.docxMelissa ShortridgeWeek 6COLLAPSEMy own attitude has ch.docx
Melissa ShortridgeWeek 6COLLAPSEMy own attitude has ch.docx
wkyra78
 
Measurement  of  the  angle  θ          .docx
Measurement  of  the  angle  θ          .docxMeasurement  of  the  angle  θ          .docx
Measurement  of  the  angle  θ          .docx
wkyra78
 
Measurement of the angle θ For better understanding .docx
Measurement of the angle θ     For better understanding .docxMeasurement of the angle θ     For better understanding .docx
Measurement of the angle θ For better understanding .docx
wkyra78
 
Meaning-Making Forum 2 (Week 5)Meaning-Making Forums 1-4 are thi.docx
Meaning-Making Forum 2 (Week 5)Meaning-Making Forums 1-4 are thi.docxMeaning-Making Forum 2 (Week 5)Meaning-Making Forums 1-4 are thi.docx
Meaning-Making Forum 2 (Week 5)Meaning-Making Forums 1-4 are thi.docx
wkyra78
 
MBA6231 - 1.1 - project charter.docxProject Charter Pr.docx
MBA6231 - 1.1 - project charter.docxProject Charter Pr.docxMBA6231 - 1.1 - project charter.docxProject Charter Pr.docx
MBA6231 - 1.1 - project charter.docxProject Charter Pr.docx
wkyra78
 
MediationNameAMUDate.docx
MediationNameAMUDate.docxMediationNameAMUDate.docx
MediationNameAMUDate.docx
wkyra78
 
Media Content AnalysisPurpose Evaluate the quality and value of.docx
Media Content AnalysisPurpose Evaluate the quality and value of.docxMedia Content AnalysisPurpose Evaluate the quality and value of.docx
Media Content AnalysisPurpose Evaluate the quality and value of.docx
wkyra78
 
MBA 5110 – Business Organization and ManagementMidterm ExamAns.docx
MBA 5110 – Business Organization and ManagementMidterm ExamAns.docxMBA 5110 – Business Organization and ManagementMidterm ExamAns.docx
MBA 5110 – Business Organization and ManagementMidterm ExamAns.docx
wkyra78
 

More from wkyra78 (20)

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

Recently uploaded

Recently uploaded (20)

Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.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
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
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
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).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; }