Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi I need help on my lexical analyzer homework using C language., So Basically we are given an input file below and we are to

Hi I need help on my lexical analyzer homework using C language.,

So Basically we are given an input file below and we are to recognize each tokens seperated by the Whitespace.

inputfile.txt

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

MyShopPurchaseOrders DEFINITIONS AUTOMATIC TAGS ::= BEGIN

PurchaseOrder ::= SEQUENCE { dateOfOrder DATE, customer CustomerInfo, items ListOfItems }

CustomerInfo ::= SEQUENCE { companyName VisibleString (SIZE (3..50)), billingAddress Address, contactPhone NumericString (SIZE (7..12)) }

Address::= SEQUENCE { street VisibleString (SIZE (5 .. 50)) OPTIONAL, city VisibleString (SIZE (2..30)), state VisibleString (SIZE(2) FROM ("A".."Z")), zipCode NumericString (SIZE(5 | 9)) }

ListOfItems ::= SEQUENCE (SIZE (1..100)) OF Item

Item ::= SEQUENCE { itemCode INTEGER (1..99999), color VisibleString ("Black" | "Blue" | "Brown"), power INTEGER (110 | 220), deliveryTime INTEGER (8..12 | 14..19), quantity INTEGER (1..1000), unitPrice INTEGER (1 .. 9999), isTaxable BOOLEAN } END

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

Alphabet Set :

a-z A-Z 0-9 " ( ) , - : = { } |

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

RESERVED WORDS:

{"AUTOMATIC", "BEGIN", "BOOLEAN", "DEFINITIONS", "END", "FROM",

"INTEGER", "NumericString", "OPTIONAL", "SEQUENCE", "SIZE", "TAGS", "VisibleString"};

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

Tokens

a. Type references

Name of lexical item typereference

A "typereference" shall consist of an arbitrary number (one or more) of letters, digits, and hyphens. The initial character shall be an upper-case letter. A hyphen shall not be the last character. A hyphen shall not be immediately followed by another hyphen.

NOTE The rules concerning hyphen are designed to a void ambiguity with (possibly following) comment.

example of typereference on the input file: MyShopPurchaseOrders , PurchaseOrder, CustomerInfo, etc..

b. Identifiers

Name of lexical item identifier

An "identifier" shall consist of an arbitrary number (one or more) of letters, digits, and hyphens. The initial character shall be a lower-case letter. A hyphen shall not be the last character. A hyphen shall not be immediately followed by another hyphen.

example of identifier on the inputfile: dateOfOrder, customer, items, companyName, etc...

c. Numbers

Name of lexical item number

A "number" shall consist of one or more digits. The first digit shall not be zero unless the "number" is a single digit.

d. Assignment lexical item

Name of lexical item "::="

This lexical item shall consist of the sequence of characters: ::=

e. Range separator

Name of lexical item ".."

This lexical item shall consist of the sequence of characters: " .. "

f. { - LURLY

g. } RCURLY

h. , COMMA

i. ( LPAREN

j. ) RPAREN

k. | BAR

l. QUOTE

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

Sample output of program:

typereference: MyShopPurchaseOrders

reserveword: DEFINITION

reserveword:AUTOMATIC

reserveword TAGS

assignment: ::=

reserveword: BEGIN

typereference: PurchaseOrder

and so on...

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

Here is my initial code:

#include

#include

#include

int main(int argc, char *argv[]){ //running it in command line LINUX

FILE *filePtr;

switch (argc) {

case 1: // No parameters, use stdin

// printf("NO argument provided ");

filePtr = stdin;

break;

case 2: // One parameter, use .txt file supplied

if ( (filePtr = fopen(strcat(argv[1], ".txt"), "r")) == NULL ) {

printf("Cannot open input file. ");

exit(1);

}

break;

default:

printf("Syntax: scanner [file] (.txt is implicit) ");

exit(0);

}

//check if empty

fseek(filePtr, 0, SEEK_END);

if (ftell(filePtr) == 0) {

printf("File is empty. ");

exit(1);

} else {

rewind(filePtr);

}

char *ReserveWord[13] = {"AUTOMATIC", "BEGIN", "BOOLEAN", "DEFINITIONS", "END", "FROM",

"INTEGER", "NumericString", "OPTIONAL",

"SEQUENCE", "SIZE", "TAGS", "VisibleString"};

char *assignment[] = {":=="};

char *range_separator[] = {".."};

char LCURLY = {'{'};

char RCURLY = {'}'};

char COMMA = {','};

char LPAREN = {'('};

char RPAREN = {')'};

char BAR = {'|'};

char QUOTE = {'"'};

int numline = 1;

char c;

while((c = fgetc(filePtr)) != EOF){

if ( c == ' '){

numline++;

}

if (c <= 65 && c >= 90 ){ //A-Z for typeref

}

}

fclose(filePtr);

}

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

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

More Books

Students also viewed these Databases questions

Question

What is a verb?

Answered: 1 week ago