开发者

how to parse from command line arguements in yacc?

how to parse from command line arguements in yacc ?

of course i undefined input in both lex & yacc and then wrote

int input(void)
{
printf("in input\n:");
char c;
if(target >  limit)
return 0;
if((c = target[0][offset++]) != '\0')
return (c);
target++;开发者_运维技巧
offset =0;
return (' ');
}

where target contains the command line arguements. But only the standard input is getting excueted how to make dis input function get executed.


Did you mean you want your generates parser accept command line arguments? Then you need to add those arguments to the main function. The lexer input is called FILE* yyin, and is initialized to stdin in the lexer. You can change the default behavior by

#include <stdio.h>
extern FILE* yyin;
int main(int argv, char** argv)
{
     if(argc==2)
     {
         yyin = fopen(argv[1], "r");
         if(!yyin)
         {
             fprintf(stderr, "can't read file %s\n", argv[1]);
             return 1;
         }
     }
     yyparse();
}

If you want your own function to be executed instead of the one provided by flex, you need to define the YY_INPUT macro.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜