I will be doing further CS coursework later this summer. My submission is a partially completed Lexical Analyzer. I would like to see it completed and used to validate the attached test program. Are y

Study Assignment for Walter W Mayo

public class NCU_PL_Lexer_Driver

public static void main(String [] args) throws java.io.IOException

{

try

{

System.out.println("Beginning lexical analysis of file \"" + args[0] + "\"");

NCU_PL_Lexer lexer = new NCU_PL_Lexer(new java.io.FileInputStream(args[0]));

lexer.NCU_PL_Program();

lexer.ReInit(new java.io.FileInputStream(args[0]));

boolean EOF = false;

while(!EOF)

{

Token token = lexer.token_source.getNextToken();

String tokenKind = NCU_PL_LexerConstants.tokenImage[token.kind];

System.out.print

(

"Token: " + tokenKind +

"\n\tLine: " + token.beginLine +

"\n\tColumn: " + token.beginColumn

);

if(tokenKind.equals("<INTEGER_LITERAL>") || tokenKind.equals("<IDENTIFIER>"))

{

System.out.print("\n\tValue: " + token.image);

}

System.out.println();

EOF = (NCU_PL_LexerConstants.tokenImage[token.kind].equals("<EOF>"));

}

System.out.println("Lexical analysis successfull");

}

catch(ParseException ex)

{

System.out.println("Error parsing input: ");

System.out.println(ex.getMessage());

}

catch(TokenMgrError ex)

{

System.out.println("Error parsing input: ");

System.out.println(ex.getMessage());

}

}


Page 2 Study assignment for Walter W Mayo

Programming Language Grammar <ncu-pl-program> ::=

“BEGIN NCUPL” <statement>* “END” <EOF>

<statement> ::=

<output-statement>

| <input-statement>

| <variable-declaration>

| <variable-assignment>

| <if-then-statement>

<expression> ::=

“(” <expression> “)”

| <identifier>

| <integer-literal>

| <string-literal>

| <boolean-literal>

| <binary-expression>

<type> ::=

“int” | “string” | “bool”

<identifier> ::=

({LETTER}1 | “_”)+

({LETTER} | {DIGIT}2 | “_”)*3

Literal Values

<integer-literal> ::=

{DIGIT}+

<string-literal> ::=

“"”{ASCII}4* “"”

<boolean-literal> ::=

“true” | “false”

Expressions

<binary-expression> ::=

<expression>

(“+” | “-“ | “-“ | “/”

| “==” | “<” | “>”)

<expression>

Statements

<variable-declaration> ::=

<type> <identifier> “;”

<input-statement> ::=

“INPUT” <expression> <identifier>5 “;”

<output-statement> ::=

“OUTPUT” <expression> “;”

<variable-assignment> ::=

<identifier> “=” <expression> “;”

<if-then-statement> ::=

“IF” <expression> “THEN” <statement>

Comments

<comment> ::=

“#” {ASCII} “\n”6

TEST PROGRAM

Page 3 Study Assignment for Walter W Mayo

BEGIN NCUPL

# Variables to keep track of years

int yearOfBirth;

int currentYear;

# Variables to keep track of months

int monthOfBirth;

int currentMonth;

# Variables to keep track of days

int dayOfBirth;

int currentDay;

# Get values for birth date

INPUT "What year were you born? " yearOfBirth;

INPUT "What month were you born? " monthOfBirth;

INPUT "What day were you born? " dayOfBirth;

# Get values for current date

INPUT "What year is it now? " currentYear;

INPUT "What month is it now? " currentMonth;

INPUT "What day is it now? " currentDay;

# Declare variables to hold/accumulate days information

int daysSince0000_01_01;

int ageDaysSince0000_01_01;

# Initialize day accumulators to 365 * year + day of month

daysSince0000_01_01 = currentYear * 365;

daysSince0000_01_01 = daysSince0000_01_01 + currentDay;

ageDaysSince0000_01_01 = yearOfBirth * 365;

ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + dayOfBirth;

##### For each month, check to see if the date is

##### greater than that month. If it is, add days

##### equal to the number of days in the month.

### January

IF (monthOfBirth > 1)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 31;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 1)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 31;

### February

IF (monthOfBirth > 2)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 28;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 2)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 28;

### March

IF (monthOfBirth > 3)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 31;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 3)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 31;

### April

IF (monthOfBirth > 4)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 30;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 4)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 30;

### May

IF (monthOfBirth > 5)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 31;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 5)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 31;

### June

IF (monthOfBirth > 6)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 30;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 6)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 30;

### July

IF (monthOfBirth > 7)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 31;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 7)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 31;

### August

IF (monthOfBirth > 8)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 31;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 8)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 31;

### September

IF (monthOfBirth > 9)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 30;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 9)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 30;

### October

IF (monthOfBirth > 10)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 31;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 10)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 31;

### November

IF (monthOfBirth > 11)

THEN ageDaysSince0000_01_01 = ageDaysSince0000_01_01 + 30;

# Repeat this logic for current month because NCUPL doesn't support functions

IF (currentMonth > 11)

THEN daysSince0000_01_01 = daysSince0000_01_01 + 30;

int ageInDays;

ageInDays = (daysSince0000_01_01 - ageDaysSince0000_01_01);

IF (ageInDays > 0) THEN OUTPUT "You are " + ageInDays + " days old (not accounting for leap years).";

IF (ageInDays < 0) THEN OUTPUT "Error: birth date must be in the past.";

END