Reputation: 137
im using visual studio 2010 for C++ i implemented a code for a lexical analyzer for C programming but i got an error says "missing type specifier - int assumed. Note: C++ does not support default-int" is there something wrong with my code? or i just have to change the compiler? here is the code
#include <stdio.h>
#include <ctype.h>
//Global 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
main(){
if ((in_fp = fopen("front.in", "r")) == NULL)
printf("ERROR - cannot open front.in \n");
else{
getChar();
do {
lex();
}while (nextToken != EOF);
}
}
// lookUp
int lookup(char ch){
switch(ch){
case '(':
addChar();
nextToken = LEFT_PAREN;
break;
case ')':
addChar();
nextToken = RIGHT_PAREN;
break;
case '+':
addChar();
nextToken = ADD_OP;
break;
case'-':
addChar();
nextToken = SUB_OP;
break;
case'*':
addChar();
nextToken = MULT_OP;
break;
case'/':
addChar();
nextToken = DIV_OP;
break;
default:
addChar();
nextToken = EOF;
break;
}
return nextToken;
}
//AddChar
void addChar(){
if (lexLen <= 98) {
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
}
else
printf("Error - lexeme is too long \n");
}
//getChar
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
void getNonBlank(){
while (isspace(nextChar))
getChar();
}
//lex
int lex(){
lexLen = 0;
getNonBlank();
switch (charClass){
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT){
addChar();
getChar();
}
nextToken = IDENT;
break;
// parse ints lits
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT){
addChar();
getChar();
}
nextToken = INT_LIT;
break;
//pares and ops
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 toke is: %d, next lexeme is %s\n",nextToken, lexeme);
return nextToken;
}
so for this example ( sum + 47 ) / total this is suppose to be the output
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
Upvotes: 0
Views: 9045
Reputation: 11889
Main is incorrectly declared.
Either it takes command line parameters:
int main(int argc, char *argv[])
{
return 0;
}
or it doesn't:
int main(void)
{
return 0;
}
Upvotes: 1
Reputation: 1271
main(){
if ((in_fp = fopen("front.in", "r")) == NULL)
printf("ERROR - cannot open front.in \n");
else{
getChar();
do {
lex();
}while (nextToken != EOF);
}
}
should be changed like this :
// return type of main should be explicitly int.
int main(){
if ((in_fp = fopen("front.in", "r")) == NULL)
printf("ERROR - cannot open front.in \n");
else{
getChar();
do {
lex();
}while (nextToken != EOF);
}
}
Upvotes: 2