[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Rookie
From: |
Werner Echezuria |
Subject: |
Rookie |
Date: |
Sun, 12 Jul 2009 14:37:13 +1930 |
Hi,
I'm trying to parse words but I can't doing it. I read the manual, but I
still can't parse something like this: KEYWORD word word, Here is my code:
%{
#include <stdio.h>
%}
%union {
char *string;
}
%token <string> KEYWORD DATA
%type <string> expr expresion
%%
input : /* empty */
| input line
;
line : '\n'
| expr '\n' { printf( "%s\n", $1 ); }
;
expr : KEYWORD expresion { $$ = $2; }
;
expresion : DATA { $$ = $1; }
;
%%
#include <stdio.h>
#include <ctype.h>
main(){
yyparse();
}
yyerror(char *str){
printf("error= %s \n",str);
}
yylex(){
int c;
while((c=getchar())==' ' || c == '\t');
if (c==EOF)
return 0;
if (isalpha (c))
{
static char *symbuf = 0;
static int length = 0;
int i;
if (length == 0)
length = 40, symbuf = (char *)malloc (length + 1);
i = 0;
do
{
if (i == length)
{
length *= 2;
symbuf = (char *)realloc (symbuf, length + 1);
}
symbuf[i++] = c;
c = getchar ();
}
while (c != EOF && isalnum (c));
ungetc (c, stdin);
symbuf[i] = '\0';
yylval.string=symbuf;
printf("%s \n",yylval.string);
return KEYWORD;
}
return c;
}
thanks for any help.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Rookie,
Werner Echezuria <=