1

Suppose I want my Lex and Yacc program to parse the command line arguments like:

./a.out show memory

I want lex to parse the string "show memory". How do I accomplish this?

||||||
2

You'll need to concatenate all the arguments into a big string, by inserting whitespace between them. Then feed the remaining text buffer to Lex/Yacc, by re-defining the YY_INPUT macro so it reads input from your text buffer.

The start could be something like:

#include <stdio.h>
#include <string.h>

char *argbuf;
size_t arglen;

int main(int argc, char *argv[])
{
  int i;

  // Compute total length of all arguments, with a single space between.
  arglen = 0;
  for(i = 1; argv[i] != NULL; i++)
    arglen += 1 + strlen(argv[i]);

  // Allocate buffer space.
  argbuf = malloc(arglen);
  if(argbuf == NULL)
  {
     fprintf(stderr, "No memory for argument buffer, aborting");
     exit(1);
  }

  // Concatenate all arguments. This is inefficient, but simple.
  argbuf[0] = 0;
  for(i = 1; argv[i] != NULL; i++)
  {
    if(i > 1)
      strcat(argbuf, " ");
    strcat(argbuf, argv);
  }

  // Here we should be ready to call yyparse(), if we had implemented YY_INPUT().

  return 0;
}
||||||
  • could u please tell me how to modify YY_INPUT in the above case i mentioned. – ajai Dec 15 '09 at 12:01
  • i am getting these errors when i try the above code try.y:8: error: syntax error before '{' token try.y:10: error: c' undeclared here (not in a function) try.y:10: error: EOF' undeclared here (not in a function) try.y:10: error: YY_NULL' undeclared here (not in a function) try.y:10: error: buf' undeclared here (not in a function) try.y:10: warning: data definition has no type or storage class try.y:11: error: syntax error before '}' token – ajai Dec 15 '09 at 12:41
  • @ajai: It's easier to help you if you simply edit your question with any additional information. – unwind Dec 15 '09 at 12:44
  • @unwind: ok as i said i want to parse the arguements which i give to executable file. for eg: ajai show memory. where ajai is the executable file and show memory are the arguements. when i redefined yy_input as given in the example still it is not reading the arguements so pls tell me how to go about it – ajai Dec 15 '09 at 13:11
  • @ajai: I meant you shouldn't post new comments, you should edit the original question, above. But perhaps you can't (due to reputation requirements), I'm not sure how it works. – unwind Dec 15 '09 at 13:18
2
int main(int argc, char **argv) {
    if(argc > 1) {
    if(argv[1])
        yy_scan_string(argv[1]);
    }
    yyparse();
    return 0;
} 
||||||
0

What's wrong with doing it the old fashioned way?:

if(argc > 1 && !strcmp(argv[1],"show"))
{
    if(argc > 2)
    {
        if(!strcmp(argv[2],"memory"))
            ...
        else if(!strcmp(argv[2],"cpu"))
            ...
        else ...
    }
}

Besides, getopt() and friends are more appropriate.

||||||
  • no i can't use dis method bcoz i have 20 keywords . moreover there is no order in which dey should occur – ajai Dec 15 '09 at 17:53
  • OK, go with unwind's method then, but I smell over engineering. – Wernsey Dec 16 '09 at 8:11
0

My blog article Parsing command line parameters with Yacc & Flex explains this with a working example. There is no need to concatenate the argument string. The reason is given in the article.

The blurb is:

Every once in a while someone comes along and asks how to parse command line parameters with Yacc & Flex. This is rather straight forward, but requires some knowledge of the generated code to get right.

Here we present a source template that does this. The user only has to edit the grammar and scanning rules. Some knowledge of C, Yacc and Flex is assumed.

The code is WTFPL licensed

The template is written for Berkeley Yacc and the reflex variant of Flex. It may be made to work with GNU Bison and SourceForge Flex, possibly with a few changes.

What you get is a template where you can just insert your lexical and grammar specification.

Please ask questions about the using and adapting the template itself to the blog comments.

||||||
  • Link to the blog is down ! Opera does not host blog anymore ! – ArthurLambert Apr 29 '14 at 14:52
  • Oh, right. And sorry about the very, very late reply. – Johann Oskarsson Mar 16 '15 at 10:17
  • I'll put the code on my new blog and fix the link one of these days. – Johann Oskarsson Mar 16 '15 at 10:18
  • And the blog post is finally re-published and the link's fixed. – Johann Oskarsson Oct 30 '17 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.