hi, we have a project to Modify the FRONT.c include the name of the token/Lexeme after the analysis. thanx in advance

I already have a file "FRONT" - which contains arithmetic operations
a = a+b+(c*d)

here is the code of FRONT.C
----------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
 //front.c - a lexical analyzer system for simple arithmetic expressions
    #include <stdio.h>
   #include <ctype.h>
     #include <conio.h>
    // Global declarations
    // Variables
    int charClass;
    char lexeme [100];
    char nextChar;
    int lexLen;
    int token;
    int nextToken;
    FILE *in_fp, *fopen();

    // Function declarations
    void addChar();
    void getChar();
    void getNonBlank();
    int lex();

    // Character classes
    #define LETTER 0
    #define DIGIT 1
    #define UNKNOWN 99

    // Token codes
    #define INT_LIT 10
    #define IDENT 11
    #define ASSIGN_OP 20

    #define ADD_OP 21
    #define SUB_OP 22
    #define MULT_OP 23
    #define DIV_OP 24
     #define LEFT_PAREN 25
     #define RIGHT_PAREN 26

     // main driver
      void main() {
	clrscr();
     // Open the input data file and process its contents
     if ((in_fp = fopen("front.in", "r")) == NULL)
 	printf("ERROR - cannot open front.in \n");
     else {
	  getchar();
     do   {
	  lex();
	} while (nextToken != EOF);
     }
	getch();

     }
     /* lookup - a function to looup operators and parentheses and return
	the token */
     int lookup (char ch) {
	switch (ch) {
		case '(':
			addChar();
			nextToken = LEFT_PAREN;
			break;
		case ')':
		

   
	addChar();
			nextToken = RIGHT_PAREN;
			break;
		case '+':
			addChar();
			nextToken = RIGHT_PAREN;
			break;
		case '-':
			addChar();
			nextToken = SUB_OP;
			break;
		case '*':
			addChar();
			nextToken = MULT_OP;
			break;
		case '/':
			addChar();
			nextToken = MULT_OP;
			break;
		default:
			addChar();
			nextToken = EOF;
			break;
		}
		return nextToken;
	}
	// addChar - a function to add nextChar to lexeme

	void addChar() {
		if (lexLen <= 98) {
			lexeme[lexLen++] = nextChar;
			lexeme[lexLen] = 0;
		      }
		else
		     printf("Error - lexeme is too long \n");
		     }
	 //************
	 /* getChar - a function to get the next character of input
		and determine its character class */
	 void getChar() {
		if ((nextChar = getc(in_fp)) != EOF) {
		    if (isalpha(nextChar))
			charClass = LETTER;
		else if (isdigit(nextChar))
			charClass = DIGIT;
		else  charClass = UNKNOWN;
		}
	   else
		charClass = EOF;
	   }


	 //*********************
	 /*getNonBlank - a function to call getChar until it returns
	   a non-whitespace character*/
	 void getNonBlank() {
		while (isspace(nextChar))
			getChar();
		}
	 //***********************
	 //lex - a simple lexical analyzer for arithmetic expressions
	 int lex() {
		lexLen = 0;
		getNonBlank();
		switch (charClass) {
		   // Parse identifiers
			case LETTER:
			addChar();
			getChar();
			while (charClass == LETTER || charClass == DIGIT) {
				addChar();
				getChar();
			}
			nextToken = IDENT;
			break;
		   // Parse integer literals
		   case DIGIT:
			addChar();
			getChar();
			while (charClass == DIGIT) {
				addChar();
				getChar();
			}
			nextToken = INT_LIT;
			break;
		   // Parentheses and operators
		   case UNKNOWN:
			lookup(nextChar);
			getChar();
			break;
		   // EOF
		   case EOF:
			nextToken = EOF;
			lexeme[0] = 'E';
			lexeme[1] = 'O';
			lexeme[2] = 'F';
			lexeme[3] = 0;
			break;
		   } //End of switch
            
               
		   printf("Next token is: %d, Next lexeme is %s\n",
		   nextToken, lexeme);
		   return nextToken;
	   } //End of function lex 
Last edited on
So what is your question?

If it's how to display the name of the token, simply move the name of the token to lexeme when you recognize the token type.

BTW, you have a missing ; at line 12.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
http://v2.cplusplus.com/articles/jEywvCM9/
It makes it easier to read your code and it also makes it easier to respond to your post.


hi, abstractionAnon tanx, here is the question
1. Modify the front.c to include the name of the token/Lexeme after the analysis

2. The analyzer does not accept assignment operation. Expand the analyzer to accept the assignment operation.

here is the given file that contains aritmetic expressions: a=a+b+(c*d)
now i need to include the name of the token/lexeme, then the analyzer should accept assignment operation. because this code thus not accept assignment operation.

this is the incomplete output of this code that we need to modify

Next token is: 11, next lexeme is a
Next token is: -1, next lexeme is =

sample file given that contains arithmetic expressions: (sum+47) / total

the output for this is:

Next token is: 25 Next lexeme is (
Next token is: 11 Next lexeme is sum
Next token is: 21 Next lexeme is +
Next token is: 10 Next lexeme is 47
Next token is: 26 Next lexeme is )
Next token is: 24 Next lexeme is /
Next token is 11 Next lexeme is total
Next token is: -1 Next lexeme is EOF

--------------
now if being modified, and include the name of the token/lexeme the output i need, would be like this:

Next token is: LEFT_PAREN Next lexeme is (
Next token is: IDENT Next lexeme is sum
Next token is: ADD_OP Next lexeme is +
Next token is: INT_LIT Next lexeme is 47
Next token is: RIGHT_PAREN Next lexeme is )
Next token is: DIV_OP Next lexeme is /
Next token is IDENT Next lexeme is total
Next token is: -1 Next lexeme is EOF

i don't think if "-1" need the name of the token. because it is not define at the beginning of the Token codes. thanx

hi, sir do you have a sample code on how to include the name of the token/lexeme after the analysis?.

I have already the solution of my #2 question on how to accept the assignment operation of the lexical analyzer.

thanx advance..
1
2
3
4
5
6
7
8
9
10
11
 void getChar() {
		if ((nextChar = getc(in_fp)) != EOF) {
		    if (isalpha(nextChar))
			charClass = LETTER;
		else if (isdigit(nextChar))
			charClass = DIGIT;
		else  charClass = UNKNOWN;
		}
	   else
		charClass = EOF;
	   }


You could of simplified that a bit. But yes that is a clean and easy code.
hi, Luke do you have a sample code on how to include the name of the token/lexeme after the analysis?. I already solved my #2 question on how to accept the assignment operation of the lexical analyzer.

here is our File that contains arithmetic operations :
a=a+b+(c*d)

here is the output that we want :

Next token is: IDENT Next lexeme is a
Next token is: ASSIGN_OP Next lexeme is =
Next token is: IDENT Next lexeme is a
Next token is: ADD_OP Next lexeme is +
Next token is: IDENT Next lexeme is b
Next token is: ADD_OP Next lexeme is +
Next token is LEFT_PAREN Next lexeme is (
Next token is: IDENT Next lexeme is c
Next token is: MULT_OP Next lexeme is *
Next token is: IDENT Next lexeme is d
Next token is: RIGHT_PAREN Next lexeme is )
Next token is: INT_LIT Next lexeme is EOF

thanx and advance..
Topic archived. No new replies allowed.