Question
Java implementation Introduction This assignment is designed to provide you with a practical understanding of the task of writing the syntax analysis parts of a
Java implementation
Introduction
This assignment is designed to provide you with a practical understanding of the task of writing the syntax analysis parts of a compiler for a high-level language called Boaz. Boaz is a language designed by me for this particular assessment so you should not expect to find any additional information about it elsewhere on the Web/Internet. This document is the definitive definition/description of Boaz.
Requirements
The main requirement of this assessment is to write a lexical analyser and recursive-descent parser for the language defined in this document. Boaz source files are stored in a text file whose name ends in a '.boaz' suffix. Your program must analyse a single Boaz source file named as its only command-line argument. Your program will only be tested with files with the correct suffix.
The grammar to which valid programs must conform is given below. There is a further challenge element, concerned with type checking, worth 20% of the marks.
The program must not output anything other than a single diagnostic message on standard output at the end of the parse. The message must be:
ok
for a successful parse or
error
for an unsuccessful parse.
A single error in the source file should cause the parser to output the error notification and stop the parse. No error recovery is required. Note that the message must be written to standard output (System.out in Java) regardless of whether the parse is successful or not.
Grammar for Boaz Here is the grammar of the Boaz language. Note that this has been expressed in a slightly different form from that in the book/slides. You should use this grammar as the basis for structuring your parser. Each rule of the grammar has a 'non-terminal' symbol on the left-hand side of the ':=' symbol. On the right-hand side of '::=' is a mix of 'terminal symbols, non-terminal symbols and grammatical notation, terminated with a semicolon. The meaning of the grammatical notation is described below. . . . The notation : := means 'is defined as'. A semicolon marks the end of each non-terminal 'rule'. Parentheses group terminal and non-terminal symbols as a single item. A ? means the single preceding item is optional or the preceding parenthesized items as a group are optional. For instance: a b ? c means that both ac and abc are valid and (a b) ? c means that both cand abc are valid. One or more items enclosed between curly brackets means the enclosed items occur zero or more times. For instance: { a b } means that ab may occur 0, 1, 2 or more times; e.g., abab, abababab, ababababababab are all valid. A separates alternatives. For instance: { a | b | c } means that either a or bor c is valid. Items all in upper-case are terminal symbols: an identifier (IDENTIFIER), keyword (BEGIN, IF, etc.), integer constant (INT_CONST) or character constant (CHAR_CONST). Items enclosed in single quotes are terminal symbols, e.g.';' and ':='. . . In the grammar be careful to distinguish between those characters not enclosed between single-quote characters (e.g., : := and ;) and those that look similar but are enclosed (e.g., ':=' and ';'). program : := PROGRAM IDENTIFIER var Decs BEGIN statements END; varDecs ::= { varDec ';' }; var Dec ::= type varList ; type ::= INT | CHAR; varList ::- IDENTIFIER { ',' IDENTIFIER }; statements ::= { statement ';' }; statement ::= assignStatement | if Statement print Statement | whileStatement ; assignStatement ::= IDENTIFIER !:=' expression ; if Statement ::- IF expression THEN statements ( ELSE statements ) ? FI ; printStatement ::= PRINT expression; whileStatement ::= WHILE expression DO statements OD; expression ::= term { binaryOp term }; binaryOp ::= arithmeticop booleanop relationalOp; arithmeticop ::- '+' '-' '*' '/'; booleanOp ::= relationalOp : := '=' | '!=' I '' . '=' ; term ::= INT CONSTI CHAR CONST | IDENTIFIER '('expression')' unaryop term; unaryop ::= I-' '!'; Comments Comments are not permitted in Boaz programs. Example program The following (nonsense) program illustrates the main syntactic features defined by the grammar. program example int num, sum; char ch; begin sum := 0; num : = 1; ch := "a"; while ch
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started