Avatar of unknown_
unknown_

asked on 

lex/flex errors

Hello,

I tried to compile my lexer but it returned back the following errors:

mygrammar.l:115: bad character: }
mygrammar.l:116: name defined twice
mygrammar.l:118: bad character: }
mygrammar.l:119: premature EOF

why do i get these errors ?

Thanks in advance for any help !
CApache Flex

Avatar of undefined
Last Comment
unknown_
Avatar of unknown_
unknown_

ASKER


%{
 
#define COMMENT		1
#define VARIABLE	2
#define INTEGER		3
#define FLOAT		4
#define STRING		5
#define T_IF		6
#define T_ELSE		7
#define T_WHILE		8
#define T_INT		9
#define T_VOID		10
#define T_RETURN	11
#define T_FLOAT		12
#define PLUS		13
#define MINUS		14
#define TIMES		15
#define SLASH		16
#define LPAREN		17
#define RPAREN		18	
#define SEMICOLON	19
#define COMMA		20
#define EQL			21
#define OR			22
#define OR2			23
#define AND			24
#define AND2		25
#define LITERAL	    26
#define IDENTIFIER	27
 
		
	
		
%}
	
	LETTER          [a-zA-Z_]
	DIGIT           [0-9]
	LETTERDIGIT     [a-zA-Z0-9_]
	SIGN            [-+]
	STRINGCONSTANT  \"[^"\n]*["\n]
    CHARCONSTANT    \'[^'\n]*\'
	RANKSPEC        \[[,]*\]
	INTEGER			{digit}+
	VARIABLE        [a-z_]({LETTERDIGIT})*
    COMMENT			"/*""/"*([^*/]|[^*]"/"|"*"[^/])*"*"*"*/"
							  
    %%
							  
  "+"                  { return PLUS;       }
  "-"                  { return MINUS;      }
  "*"                  { return TIMES;      }
  "/"                  { return SLASH;      }
  "("                  { return LPAREN;     }
  ")"                  { return RPAREN;     }
  ";"                  { return SEMICOLON;  }
  ","                  { return COMMA;      }
  "="                  { return EQL;        }
  "|"                  { return OR;         }
  "||"                 { return OR2;        }
  "&"                  { return AND;        }
  "&&"                 { return AND2;       }
  
  "if"                 { return T_IF;       }
  "else"               { return T_ELSE;     }
  "do"                 { return T_DO;       }
  "int"                { return T_INT;      }
  "return"             { return T_RETURN;   }
  "void"               { return T_VOID;     }
  "float"              { return T_FLOAT;    }
  "while"              { return T_WHILE;   }
  
  
  {LETTER}{LETTERDIGIT}* {
  return(IDENTIFIER);
  }
  
  {VARIABLE}* { 
	  printf("VARIABLE\n"); yylval.lexeme=(char*)malloc(yyleng+1);
	  strcpy(yyval.lexeme, yytext); 
	  /*return T_VARIABLE*/
	  return(LITERAL);
  }
  
  {COMMENT} {
	return(LITERAL);
  }
  
  {SIGN}?{DIGIT}+"."{DIGIT}+ {   
	  printf("FLOAT\n"); sscanf(yytext,"%d", &(yyval.value));
	  return(LITERAL);
  }
  
  {SIGN}?{DIGIT}+ {  
	  printf("INTEGER\n"); sscanf(yytext,"%d", &(yyval.value));
	  return(LITERAL);
  }
  
  {STRINGCONSTANT} {
  return(LITERAL);
  }
  
  
  [ \t\n\r]            /* skip whitespace */
  
  .                    { printf("Unknown character [%c]\n",yytext[0]);
						return UNKNOWN;    }
 
	%%
	
int main(void){
							  int token;
							  while(token = yylex()) {
							  printf("lexed token: %d\n", token);
							  }
}
int yywrap(void){
	return 1;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of mrjoltcola
mrjoltcola
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of unknown_
unknown_

ASKER

Thank you very much !
C
C

C is a general-purpose, imperative computer programming language, supporting structured programming, lexical variable scope and recursion, while a static type system prevents many unintended operations. By design, C provides constructs that map efficiently to typical machine instructions, so it has found lasting use in applications that had formerly been coded in assembly language, including operating systems as well as various application software for computers ranging from supercomputers to embedded systems. It is distinct from C++ (which has its roots in C) and C#, and many later languages have borrowed directly or indirectly from C.

23K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo