Starting from:

$25

ECS140A-Project 2 Solved

. For this project you will be writing a Java program to parse language X.  Language X is similar to the C language, but is simpler and slightly different.  For this program you will be reading in an X language file and converting it to a syntax highlighted XHTML version.  A CSV file will describe the font and color settings for the XHTML output file.  The CSV file will have a .csv extension and the X language file will have a .x extension. You will be creating several classes that will solve the various portions of the problem, and reusing the classes developed in Project 1.  

X   Programming Language
The following EBNF describes the X language. The Non-terminals on the right are identified by italics.  Literal values are specified in bold.  The operators not in bold or italics describe the options of the EBNF.  These operators are {} for repetition, [] for optional, () for grouping, and | for or.

EBNF Rules: 

Program := {Declaration} MainDeclaration {FunctionDefinition} 

Declaration := DeclarationType (VariableDeclaration | FunctionDeclaration) 

MainDeclaration := void main ( ) Block  

FunctionDefinition := DeclarationType ParameterBlock Block 

DeclarationType := DataType Identifier 

VariableDeclaration := [= Constant] ; 

FunctionDeclaration := ParameterBlock ; 

Block := { {Declaration} [(Statement {Statement} {FunctionDefinition})] } 

ParameterBlock := ( [Parameter {, Parameter}] ) 

DataType := IntegerType | FloatType 

Constant := IntConstant | FloatConstant 

Statement := Assignment | WhileLoop | IfStatement | ReturnStatement | (Expression ;) 

Parameter := DataType Identifier 

IntegerType := [unsigned] ( char | short | int | long ) 

FloatType := float | double 

Assignment := Identifier = {Identifier =} Expression ; 

WhileLoop := while ( Expression ) Block 

IfStatement := if ( Expression ) Block 

ReturnStatement := return Expression ; 

Expression := SimpleExpression [ RelationOperator SimpleExpression ]  

SimpleExpression := Term { AddOperator Term } 

Term := Factor { MultOperator Factor } 

Factor := ( ( Expression ) ) | Constant | (Identifier [ ( [ Expression {, Expression}] ) ] ) 

RelationOperator := ( == ) | < | | ( <= ) | ( = ) | ( != ) 

AddOperator := + | -  

MultOperator := * | / 

Token Rules: 

 

Identifier := ( _ | Alpha ) { ( _ | Digit | Alpha )  } 

IntConstant := [ - ] Digit { Digit } 

FloatConstant := [ - ] Digit { Digit } [ . Digit { Digit } ] 

Digit := 0 – 9 

Alpha := A – Z | a - z 

The X programming language allows for nested function declarations. Variables and functions must be declared before they are referenced or called.  Identifiers have the same scoping rules as C. An identifier may only be declared once per block but may be declared more than once per file.   

Output XHTML
The output XHTML will output the X language file with fonts and colors specified by the CSV file. All token types that are not specified in the CSV file will be assumed to be the same as the browser defaults. The token classes are IntConstants, FloatConstants, Keywords, Operators, Variables, and Functions. The keywords of language X are: unsigned, char, short, int, long, float, double, while, if, return, void, and main.  The operators of language X are: (, ,, ), {, }, =, ==, <, , <=, =, !=, +, -, *, /, and ;.  All Variable and Function references must link back to the declaration of the Variable or Function.  Each nested block should be indented another level, the amount of indention will 4 spaces.

Input CSV File
The input CSV description file describes the formatting to be used for the XHTML file.  The default will be specified by the DEFAULT ELEMENT_TYPE. The attribute types are:

BACKGROUND,     FOREGROUND,    and    STYLE.    The     DEFAULT                                   BACKGROUND,

FOREGROUND, and STYLE attributes set the global background color, foreground color and font face for the output XHTML. The indention is 4 spaces for each nested block. The FOREGROUND and STYLE may be specified for the token types: FUNCTION, VARIABLE, FLOAT_CONSTANT, INT_CONSTANT, OPERATOR, and  KEYWORD. Below is an example of CSV formatting file.

ELEMENT_TYPE,BACKGROUND,FOREGROUND,STYLE,FONT 

DEFAULT,navy,yellow,,"Courier New" 

FUNCTION,,orange,, 

VARIABLE,,yellow,, 

FLOAT_CONSTANT,,aqua,b, 

INT_CONSTANT,,aqua,b, 

OPERATOR,,white,b, 

KEYWORD,,white,b, 

 

Suggested Approach  
1.     Write a class that will open and read the .csv file as specified on the command line. You should use your CSVParser class from Project 1. Make sure you can determine the formatting for each element type.

2.     Use your Scanner class from Project 1 to write a recursive descent parser class that will parse the .x file by making repeated calls to the getNextToken() method. (You may need to use peekNextToken() to look ahead in some instances.) This class should implement one method per EBNF rule.  It should also be able to validate that the .x file does conform to the grammar specified.  If an error is found in the .x file, the line number of the error and the grammar rule where the error was found should be printed with an appropriate message. A working example is available on the CSIF to test against.

3.     Using the classes written in steps 1 and 2, write a program that reads in a .x source file and a .csv file and outputs the XHTML formatting. The colors and styles of tokens in the XHTML file should match the specification in the .csv file. In addition, references to variables and functions in the source file should be hyperlinks back to their respective declarations. Do not worry about matching overloaded functions with appropriate call, just link back to the function that would be in current scope as if it were a variable. The XHTML should be a valid HTML that can be loaded into any web browser and viewed. You will probably need to begin your XHTML with:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1transitional.dtd"

You may also need to have attributes in your html element. So it should look like: <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"

Examples of valid input files .x and .csv files are in /home/cjnitta/ecs140a/proj2 on the CSIF. Scripts that run the example classes can be found there too. All scripts take one argument except for XFormatter.sh which takes two arguments, the format CSV file and then the X file to format. Your code will be tested on the CSIF and is expected to compile and run on the CSIF.

More products