Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

URGENT - NEED HELP ASAP - I WILL UPVOTE I started this project but I'm having trouble getting my code to run, can someone fix

URGENT - NEED HELP ASAP - I WILL UPVOTE

I started this project but I'm having trouble getting my code to run, can someone fix it? My code along with the instructions are below:

The goal of this assignment is the development of a scanner for a subset of the SCL language. The programming language must be Java. The scanner implementation must include an array of the keywords used in the subset of SCL, an array (or list) of the identifiers. Define the grammar of a subset of SCL. Show the execution of this scanner program by using appropriate input files, the program must show a list of the tokens scanned.

grammar for the (subset of SCL) language Syntax Rules, see scl_subset.yy

Lexical Rules

identifier letter char_list char_list letter_digit char_list | letter_digit

literal_integer digit literal_integer | digit assignment_operator = le_operator <= lt_operator <

ge_operator >=

gt_operator > eq_operator = = ne_operator ~= add_operator + sub_operator - mul_operator * div_operator /

///// scl_subset.yy

%{ /* SCL A Scientific Computing Language // for the low-level programming

// First implemented as a pre-processor that generates C programs // Subset of SCL specified for YACC (Bison)

start : symbols forward_refs specifications globals implement ; symbols : | symbols symbol_def ; symbol_def : SYMBOL IDENTIFIER HCON ; forward_refs : | FORWARD frefs ; frefs : REFERENCES forward_list | forward_list ; forward_list : forwards | forward_list forwards ; forwards : | check_ext func_main dec_parameters ; check_ext : | MEXTERN ; func_main : | FUNCTION IDENTIFIER oper_type | MAIN {dec_main();} ; oper_type : RETURN chk_ptr chk_array ret_type ; chk_ptr : | POINTER {pointer_flag = true;} ; chk_array : | ARRAY array_dim_list ; array_dim_list : LB array_index RB | array_dim_list LB array_index RB ; array_index : IDENTIFIER | ICON ;

ret_type : TYPE type_name | STRUCT IDENTIFIER | STRUCTYPE IDENTIFIER ; type_name : MVOID | INTEGER | SHORT | REAL | FLOAT | DOUBLE | TBOOL | CHAR | TSTRING OF LENGTH ICON | TBYTE ;

specifications : | SPECIFICATIONS spec_list ; spec_list : spec_def | spec_list spec_def ; spec_def : ENUM | STRUCT ; globals : | GLOBAL declarations ; declarations : | DECLARATIONS ; implement : IMPLEMENTATIONS funct_list ; funct_list : funct_def | funct_list funct_def ; funct_def : funct_body ; funct_body: FUNCTION main_head parameters f_body ; main_head : MAIN | IDENTIFIER ; parameters : | PARAMETERS param_list ; param_list : param_def | param_list COMMA param_def ; param_def : identifier chk_const chk_ptr chk_array OF TYPE type_name ; chk_const : | CONSTANT ; f_body : BEGIN ENDFUN ; statement_list : statement | statement_list statement ; statement : if_statement | assignment_statement | while_statement | print_statement | repeat_statement ; if_statement : IF boolean_expression THEN statement_list ELSE statement_list ENDIF ;

while_statement : WHILE boolean_expression DO statement_list ENDWHILE ;

assignment_statement : LET identifier assignment_operator arithmetic_expression ;

repeat_statement : REPEAT statement_list UNTIL boolean_expression ENDREPEAT ;

print_statement : DISPLAY arg_list ; arg_list : args | arg_list comma args ; args : identifier | constant | string ; boolean_expression : arithmetic_exp relative_op arithmetic_exp ;

relative_op : le_operator | lt_operator | ge_operator | gt_operator | eq_operator | ne_operator ;

arithmetic_exp : arithmetic_exp add_operator mulexp | arithmetic_exp sub_operator mulexp | mulexp ; mulexp : mulexp mul_operator primary | mulexp div_operator primary | primary ; primary : left_paren arithmetic_exp right_paren | minus primary | constant | identifier ;

-------------------------------------------------

MY CODE:

Lexer.java

public class Lexer { LinkedList tokenList; ArrayList keywords; ArrayList error; HashMap hashMap= new HashMap(); //Scanner class public Lexer(String filename) { //Check if text file is empty assert(filename != null); //Include a list of the keywords used in subset of SCL //Keywords are defined in text file with name keywords keywords = new ArrayList(); error = new ArrayList(); //Add a keyword and the token type associated with the keyword try{ Scanner keywordFile = new Scanner(new File("Keywords.txt")); while(keywordFile.hasNext()) { String line = keywordFile.nextLine(); int index = removeWhiteSpaces(line,0); String key = separateKeyword(line, index); key.toLowerCase(); //Get the keyword length and a white space index += key.length()+1; String typeString=line.substring(index,line.length()); TokenType type = TokenType.valueOf(typeString.toUpperCase()); hashMap.put(key,type); } keywordFile.close(); } catch(Exception e) { System.out.println(e); System.exit(0); } //Get tokens //Compute each line //Put into token array tokenList = new LinkedList(); int stringNum = 0; // try{ Scanner scan = new Scanner(new File(filename)); while(scan.hasNext()){ String line = scan.nextLine(); computeLine(stringNum, line); stringNum++; } tokenList.add(new Token(stringNum,"EOF",TokenType.EOF_TOKEN)); scan.close(); } catch(Exception e) { System.out.println(e); System.exit(0); } } //Using the helper method, check each line to obtain a lexeme and token private void computeLine(int stringNum, String line) { assert(stringNum >= 0); assert(line != null); int index = 0; index = removeWhiteSpaces(line,index); //Check each line while(index < line.length()){ String lexeme = getLexeme(line,index); lexeme = lexeme.toLowerCase(); TokenType type = getTokenType(lexeme,stringNum); index += lexeme.length(); index = removeWhiteSpaces(line,index); tokenList.add(new Token(stringNum,lexeme,type)); } } //Find lexemes in the string private String getLexeme(String line, int index) { assert(line != null && index >= 0); int end = index; while(end < line.length() && !isWhiteSpace(line.charAt(end))){ end++; } String lex = line.substring(index,end); return lex; } //Find keywords in the string private String separateKeyword(String line, int index){ assert(line != null && index >= 0); int end = index; while(end=0); TokenType token = null; //Either a special keyword or an identifier if(isLetter(lexeme.charAt(0))){ if(lexeme.length()==1){ token = TokenType.IDEN_TOKEN; }else if(hashMap.containsKey(lexeme)){ token = hashMap.get(lexeme); }else{ error.add("Invalid lexeme:"+lexeme+" on line "+stringNum); } //Check if all lexemes are digits //If lexeme is not a digit, then return error } else if(isDigit(lexeme.charAt(0))) { int index=1; boolean checkDigit = true; while(index')) { token = TokenType.RELATIVE_OP_TOKEN; } else if(lexeme.charAt(0) == '=') { token = TokenType.ASSIGNMENT_OP_TOKEN; } else if(lexeme.charAt(0) == '(') { token = TokenType.LEFT_PAREN_TOKEN; } else if(lexeme.charAt(0) == ')') { token = TokenType.RIGHT_PAREN_TOKEN; } } if(token == null && lexeme.length() == 2){ if(((lexeme.charAt(0)=='<' && lexeme.charAt(1)=='=') || (lexeme.charAt(0)=='>' && lexeme.charAt(1)=='=') || (lexeme.charAt(0)=='=' && lexeme.charAt(1)=='=') || (lexeme.charAt(0)=='~' && lexeme.charAt(1)=='='))){//check relatives 2 char //relative op token=TokenType.RELATIVE_OP_TOKEN; } } if(token == null){ token = TokenType.INVALID_TOKEN; } return token; } //Removes white spaces in the string private int removeWhiteSpaces(String str,int index){ assert(str!= null && index>=0); while(index='a' && letter<='z'){ return true; } else if (letter>='A' && letter<='Z'){ return true; } else { return false; } } //Determines if the input is a digit between 0 and 9 private boolean isDigit(char num){ if(num>='0' && num<='9'){ return true; } return false; } //Displays a list of tokens scanned public LinkedList getTokenList(){ if(!error.isEmpty()){ this.printError(); throw new IllegalArgumentException("Invalid Lexeme."); } return this.tokenList; } //After an error is discovered //An appropriate error message is printed public void printError(){ Iterator iterate = error.iterator(); while(iterate.hasNext()){ String err = (String)iterate.next(); System.err.println(err); } } }

TokenType.java

//This file contains the grammar from the document //Below the token types are described //Enum TokenType public enum TokenType { PROGRAM_TOKEN, BLOCK_TOKEN, IDEN_TOKEN, INVALID_TOKEN, EOF_TOKEN, // ------ StatementPrograms.Statement tokens ---------- STATEMENT_TOKEN, IF_STATE_TOKEN, THEN_STATE_TOKEN, ELSE_STATE_TOKEN, WHILE_STATE_TOKEN, DO_STATE_TOKEN, FOR_STATE_TOKEN, FUNCTION_STATE_TOKEN, END_STATE_TOKEN, UNTIL_STATE_TOKEN, PRINT_STATE_TOKEN, REPEAT_STATE_TOKEN,//Keywords ASSIGNMENT_STATE_TOKEN,//ID operation assignment // ------ Expression tokens --------- BOOLEAN_EXPR_TOKEN,//Relative, arithmetic ARITH_EXPR_TOKEN,//Arithmetic expression RELATIVE_OP_TOKEN,//>, <, ==, /=, >=, <= // ------ Operation tokens ----------- ARITH_OP_TOKEN,//+,-,*,/ ASSIGNMENT_OP_TOKEN,//= LITERAL_INT_TOKEN,//Digit LEFT_PAREN_TOKEN,//Left parenthesis ( RIGHT_PAREN_TOKEN,//Right parenthesis ) } 

Token.java

//Token MemoryClass public class Token{ private int rowNumber; private int colNumber; private TokenType tkn; private String lexeme; //Constructor for Token MemoryClass public Token(int rowNumber, String lexeme, TokenType tkn){ //Initialize lexeme, row number, column number, and token type if(rowNumber < 0 || lexeme == null || tkn == null || lexeme.equals("")) { throw new IllegalArgumentException("Invalid inputs for token Lexeme:"+lexeme+" rowNumber:"+rowNumber+" TokenType:"+tkn); } this.rowNumber=rowNumber; this.tkn = tkn; this.lexeme = lexeme; } //Return results public String getLexeme(){ return this.lexeme; } public int getRowNumber(){ return this.rowNumber; } public TokenType getTokenType(){ return this.tkn; } public String toString(){ return "Line Number:"+this.getRowNumber()+" Lexeme Value:"+this.getLexeme()+" Token Type:"+this.getTokenType(); } } Keywords.txt
if IF_STATE_TOKEN then THEN_STATE_TOKEN else ELSE_STATE_TOKEN while WHILE_STATE_TOKEN do DO_STATE_TOKEN for FOR_STATE_TOKEN function FUNCTION_STATE_TOKEN end END_STATE_TOKEN repeat REPEAT_STATE_TOKEN until UNTIL_STATE_TOKEN print PRINT_STATE_TOKEN

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Main Memory Database Systems

Authors: Frans Faerber, Alfons Kemper, Per-Åke Alfons

1st Edition

1680833243, 978-1680833249

More Books

Students also viewed these Databases questions

Question

Understand the principles of a learning organization.

Answered: 1 week ago

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago