[Flex/Bison] Some questions

This is a discussion on [Flex/Bison] Some questions within the C Programming forums, part of the General Programming Boards category; Question 1 So, as I mentioned in my previous thread, I'm trying to make a parser for a made-up programming ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    53

    [Flex/Bison] Some questions

    Question 1
    So, as I mentioned in my previous thread, I'm trying to make a parser for a made-up programming language. I'm now at the part of the exercise where we're required to make sure the parser's output is a conversion in C of the input.

    So things like...

    Code:
    STARTMAIN a=b+2; return a; ENDMAIN
    ...must become...

    Code:
    int main () { a=b+2; return a; }
    So far so good, almost. The exercise also requires that in the same time, as we convert, we have to add proper indentation and (as I had to learn the hard way last year) newlines.

    The obvious part is that each time a { opens, you increase a counter and then add the appropriate tabs on each new line. However, closing brackets ('}') are a different story as you can't detect them before hand, and once you've parsed them, you can't just put them a tab to the left by removing the last tab printed.

    Is there a solution to this, and/or a consistent way of checking and adding indentation?

    Question 2
    Also, I have tried something like this in my Bison file...

    Code:
    ReturnS: RETURN expression {printf(";")}
    ...but the semicolon gets printed AFTER the next token, past this rule, instead of right after the expression. This rule was made as the original language doesn't require a semicolon after the expression in the return statement, but C does, so I thought I'd add it manually to the output with printf, but that doesn't seem to work, as the semicolon gets added but for some reason, it gets added after the next token is parsed (outside the ReturnS rule) instead of right when the expression rule returns to ReturnS.
    Last edited by Leftos; 08-21-2010 at 03:43 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Haven't read your previous thread, but can't you use a buffer and save some things before you print them?

    This
    Code:
    for (int i = 0; i < counter; ++i) 
      printf("\t");
    is a sample code of how you would indent. Assuming you use something like that, why can't you add a flag like
    [CODE[
    if (NOT_END)
    for (int i = 0; i < counter; ++i)
    printf("\t");
    [/CODE]
    so you don't do this if you are dealing with ENDMAIN style of commands.
    You print the above when you print the expression, not in advance.

    For questions 2 a wild guess is that it is recognizing the whole return statement as RETURN. So like RETURN would be "return val;" for example. Just guessing, not even know Bison/Flex that well

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    53
    Thanks for your input C_ntua. Here's a third question though.

    Question 3
    I'm trying to do something like this in Bison...

    Code:
    loop_for:	FOR var_name COLONEQUALS expression TO {printf("%s<=", $2);} expression STEP {printf("%s+=", $2);} expression {printf(")\n");} Code ENDFOR
    What I'm trying to do is convert a for statement from the fake langugage's syntax to C's. However, the $2 I've used to grab var_name doesn't seem to work as the program crashes when it reaches there. Is $x supposed to work only for integers?

    I even tried adding a union and using char* for a new type, but I still get (null) from the above actions.
    Last edited by Leftos; 08-21-2010 at 04:27 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc problem
    By Tool in forum C Programming
    Replies: 23
    Last Post: 03-12-2010, 09:54 AM
  2. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  3. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  4. Several Questions, main one is about protected memory
    By Tron 9000 in forum C Programming
    Replies: 3
    Last Post: 06-02-2005, 07:42 AM
  5. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 08:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21