Scanning and parsing using lex and yacc


Project Phase1: Scanning and Parsing using Lex and Yacc

1 AA Language

For this phase, you will use Lex tool for writing the scanner and Yacc tool for writing the parser for a programming language called AA.

Part 1: The Scanner and the Lexemes:

  • Keywords: int, double, if, read, print, while,START,END.
  • Operators:

- Arithmetic: + , - , * , /

- Logic: &&,||,!

- Relational: ==, <>, <, <=, >,>=

- Assignment: =

- Punctuation elements: (),,,{}

Precedence and associativity

The table below shows all AA operators from highest to lowest precedence, along with their associativity.

Table 1: precedence and associativity table

Operator

Description

Associativity

!

Unary Logical NOT

right to left

/

Multiplication/Division

left to right

+-

Addition/ Subtraction

left to right

<, <=, >,>=

Relational

left to right

=<>

Equality

left to right

&&

Logical AND

left to right

||

Logical OR

left to right

=

Assignment

right to left

Identifiers: should start with capital letter and can include small, capital letters, underscores, or digits.

  • Comment: is sequence of any characters between double hash ##. ##

Literals: INT LITERAL e.g. 12345, STRING LITERAL e.g. "Hello"and DBL LITERAL e.g. 3.14.

Part 2: The Parser and the Grammar:

When YACC finds input that doesn't match the grammar, it automatically termi- nates with the message 'Syntax error'. You will need to implement an error routine (yyerror) that also prints out the line number before this termination, and a main method that read AA code from a file. Print an appropriate message in case of no syntax error. Build your parser using the grammar below:

Start→ Program START Statements END

Statements → Statements Statement| Statement

Statement→ Dec stmt | Assignment stmt | Print stmt | Read stmt

| Condition stmt | While stmt

Dec stmt→ Type ID |Type ID,IDList IDList → ID | ID,IDList

Type→ int | double

Assignment stmt→ID = Expression

Expression → exp == exp | exp <> exp | exp < exp | exp <= exp| exp >= exp | exp >= exp | exp

exp→ exp + exp | exp - exp| exp * exp | exp /exp | exp || exp | exp && exp | !exp | Factor

Factor→ ( exp )| INT LITERAL | DBL LITERALL | ID Print stmt→ print ( ID ) | print ( STRING LITERAL ) Read stmt→ ID = read ( )

Condition stmt→ if (Expression ) statements While stmt→ while ( Expression ) statements

Deliverable and rules

Write the Lex and Yacc file for AA language as described above. Once you built your parser, you should be able to parse AA programs. If you want to test your code, consider the following program.

Start

int X12 double ABC1 DDe =7

while ( QNn >0) ## this a Comment ##

{ RLk 9999 = ACc - 2

CCC = True

if( ACc == 5){ print (" Inside . IF. inside . Loop ") } } print (" Hello ")

End

This program should be compiled and have no errors. Note that this program does not cover all functionalities that could be tested based on the given specification. Test your code thoroughly. If the input program is in different syntax than the given grammar, your parser should report an error.

We, as Programming Languages and Compilation Assignment Help service is the leading online service provider organization in the industry and are mostly preferred by the students.

Tags: Programming Languages and Compilation Assignment Help, Programming Languages and Compilation Homework Help, Programming Languages and Compilation Coursework, Programming Languages and Compilation Solved Assignments

Attachment:- Programming Languages and Compilers.rar

Request for Solution File

Ask an Expert for Answer!!
Theory of Computation: Scanning and parsing using lex and yacc
Reference No:- TGS03046682

Expected delivery within 24 Hours