Basically this is a program which I take a text file with some code and take each char and make it a token or make the other chars that are included in it become a token like a variable. That part works I know because I have used this code for another project and I’m just reusing. The problem is that when I am trying to get the token the scanner.c create and return it to another file it is saying I have an error “error: expected identifier or ‘(’ before ‘TOKEN’”. I cant figure out where the error is. Here is my scanner.c file and I also posted the .h file where the token is defined.
#include <stdio.h>
#include <string.h>
#include "parser.h"
*TOKEN parseString(char *token);
*TOKEN parseChar(char token);
*TOKEN scan(FILE *input_file)
{
int stateTable[3][122];
int i = 0;
int j = 0;
for( i=0; i < 3; i++)//init to 0
{
for ( j=0; j < 122; j++)
{
stateTable[i][j] = 0;
}
}
for( i=48; i < 57; i++)//all numbers [0-9]
{
stateTable[1][i] = 3;
stateTable[3][i] = 3;
}
for( i=97; i < 122; i++)//all alpha [a-z]
{
stateTable[1][i] = 2;
stateTable[2][i] = 2;
}
stateTable[1][10] = 1;// \n
stateTable[1][32] = 1;// space
stateTable[1][40] = 1;// (
stateTable[1][41] = 1;// )
stateTable[1][42] = 1;// *
stateTable[1][43] = 1;// +
stateTable[1][45] = 1;// -
stateTable[1][47] = 1;// /
stateTable[1][59] = 1;// ;
stateTable[1][61] = 1;// =
int state = 0;
int index = 0;
char temp[20];
char token;
token = fgetc(input_file);
state = stateTable[1][(int) token];
if (state == 2 || state == 3)//num or alpha
{
temp[index] = token;
index++;
scan(input_file);
}
else if (state = 1)
{
if (index >= 1)//digit or identifier
{
temp[index] = '\0';
return parseString(&temp[0]);
temp[0] = '\0';
}
else
return parseChar(token);
if(token == ';' ||token == ')')
return parseChar(token);
index = 0;
}
}
*TOKEN parseString(char *token)
{
TOKEN *temp = malloc(sizeof(TOKEN));
if(strcmp("repeat",token) == 0)
&temp.type = 4;
else if(strcmp("print",token) == 0)
&temp.type = 5;
else if(isdigit(token[0]))
{
&temp.type = 12;
&temp.attribute = token[0];
}
else if(strcmp(token, "") != 0)
{
&temp.type = 10;
&temp.attribute = token;
}
return temp;
}
*TOKEN parseChar(char token)
{
TOKEN *temp = malloc(sizeof(TOKEN));
if(token == '+' || token == '-')
{
&temp.type = 14;
&temp.attribute = token;
}
else if(token == '/' || token == '*' || token == '%')
{
&temp.type = 13;
&temp.attribute = token;
}
else if(token == '=')
&temp.type = 3;
else if(token == ';')
&temp.type = 17;
else if(token == '(')
&temp.type = 15;
else if(token ==')')
&temp.type = 16;
return temp;
}
Here’s the parser.h file that has the TOKEN in it described.
#ifndef _parser_h
#define _parser_h
typedef enum token_type
{
Id,
keyword,
num,
addOp,
multOp,
assignment,
semicolon,
lparen,
rparen
}TOKEN_TYPE;
typedef struct token{
TOKEN_TYPE type;
char* attribute;
}TOKEN;
typedef enum node_type
{
PROGRAM,
statement,
assignStmt,
repeatStmt,
printStmt,
exp,
term,
factor
}NODE_TYPE;
#endif
It just keeps saying where ever I declare the function name that I am missing a ‘(‘ before the TOKEN.
Any help would be great.
should be
TOKEN* parseChar(char token)
instead of
*TOKEN parseChar(char token)
and
temp->type = 4;
instead of
&temp.type = 4;