Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

ID: 3880083 • Letter: H

Question

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 <stdio.h>

#include <stdlib.h>

#include <string.h>

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);

}

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

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

FILE *filePtr;

int initial_index=0,i; // Index of storing data
char store[100]; //To store data retrived from file

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
  
}

if(c == ' ') //To check for the spaces
{
for(i=0;i<initial_index;i++)
printf("%c",store[i]);
printf(" ");
initial_index=0; //Reset to initial index
}
else //Storing file data
store[initial_index++]=c;
}

fclose(filePtr);

}

// Please go through the code first

In the code, I have added one array to store the data and one more extra variable. Also added one more if condition to check whether the retrieved character is space or not.