Generate Parse Tree in c









up vote
0
down vote

favorite












The following code is supposed to generate a parse tree of the input expression, but the problem is that the output E,T,F,S (functions used in the code). I want it to be something like:




a+b*c => E*c => E+b*c => a+b*c




 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);void T(void);
void S(void);void F(void);
void error(int);void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;



//The main should always be very simple
//First scan the string
//second check for end of string reached , if yes success and if not error.

//P ---> E '#'
int main(void)
printf("Input:");
scan(); E();
if (next != '#') error(1);
else printf("***** Successful parse *****n");


//E ---> T '-') T
void E(void) next == '-')
scan();
T();

leave('E');



//T ---> S '/') S
void T(void)


//S ---> F '^' S | F
void S(void)

enter('S'); F();
if (next == '^')
scan(); S();

leave('S');


//F ---> char | '(' E ')'
void F(void)

enter('F');
if (isalpha(next))

scan();

else if (next == '(')
scan(); E();
if (next == ')')
scan();
else
error(2);

else
error(3);

leave('F');

//Scan the entire input
void scan(void)
while (isspace(next = getchar()));


void error(int n)

printf("n*** ERROR: %in", n);
exit(1);


void enter(char name)

spaces(level++);
printf("+-%cn", name);


void leave(char name)

spaces(--level);
printf("+-%cn", name);


//TO display the parse tree
void spaces(int local_level)
");










share|improve this question























  • I am thoroughly confused. What do you mean by, "the problems is that the output E,T,F,S"? (I see nothing resembling that output when I run it on the same input.) How is a+b*c equivalent to E*c? Why do you end up with the same expression as you started with? How is the sequence you want related to the way the program generates output (which seems to be correct, btw)?
    – Marcelo Cantos
    Jun 28 '13 at 0:39











  • actually a+bc is just an example, and consider it as tree so ''precedence is high it will evaluate first and left part will be considered as expression i.e 'E' , similarly a+b and you of course you get the same expression in tree as in actual.
    – Ar Khan
    Jun 28 '13 at 2:34










  • the problem is that this code is giving output in the form of E,T,F,S , when it takes input expression then it should give out of same expression... :/ i am confused with that what changes should be made to do that... :)
    – Ar Khan
    Jun 28 '13 at 2:39














up vote
0
down vote

favorite












The following code is supposed to generate a parse tree of the input expression, but the problem is that the output E,T,F,S (functions used in the code). I want it to be something like:




a+b*c => E*c => E+b*c => a+b*c




 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);void T(void);
void S(void);void F(void);
void error(int);void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;



//The main should always be very simple
//First scan the string
//second check for end of string reached , if yes success and if not error.

//P ---> E '#'
int main(void)
printf("Input:");
scan(); E();
if (next != '#') error(1);
else printf("***** Successful parse *****n");


//E ---> T '-') T
void E(void) next == '-')
scan();
T();

leave('E');



//T ---> S '/') S
void T(void)


//S ---> F '^' S | F
void S(void)

enter('S'); F();
if (next == '^')
scan(); S();

leave('S');


//F ---> char | '(' E ')'
void F(void)

enter('F');
if (isalpha(next))

scan();

else if (next == '(')
scan(); E();
if (next == ')')
scan();
else
error(2);

else
error(3);

leave('F');

//Scan the entire input
void scan(void)
while (isspace(next = getchar()));


void error(int n)

printf("n*** ERROR: %in", n);
exit(1);


void enter(char name)

spaces(level++);
printf("+-%cn", name);


void leave(char name)

spaces(--level);
printf("+-%cn", name);


//TO display the parse tree
void spaces(int local_level)
");










share|improve this question























  • I am thoroughly confused. What do you mean by, "the problems is that the output E,T,F,S"? (I see nothing resembling that output when I run it on the same input.) How is a+b*c equivalent to E*c? Why do you end up with the same expression as you started with? How is the sequence you want related to the way the program generates output (which seems to be correct, btw)?
    – Marcelo Cantos
    Jun 28 '13 at 0:39











  • actually a+bc is just an example, and consider it as tree so ''precedence is high it will evaluate first and left part will be considered as expression i.e 'E' , similarly a+b and you of course you get the same expression in tree as in actual.
    – Ar Khan
    Jun 28 '13 at 2:34










  • the problem is that this code is giving output in the form of E,T,F,S , when it takes input expression then it should give out of same expression... :/ i am confused with that what changes should be made to do that... :)
    – Ar Khan
    Jun 28 '13 at 2:39












up vote
0
down vote

favorite









up vote
0
down vote

favorite











The following code is supposed to generate a parse tree of the input expression, but the problem is that the output E,T,F,S (functions used in the code). I want it to be something like:




a+b*c => E*c => E+b*c => a+b*c




 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);void T(void);
void S(void);void F(void);
void error(int);void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;



//The main should always be very simple
//First scan the string
//second check for end of string reached , if yes success and if not error.

//P ---> E '#'
int main(void)
printf("Input:");
scan(); E();
if (next != '#') error(1);
else printf("***** Successful parse *****n");


//E ---> T '-') T
void E(void) next == '-')
scan();
T();

leave('E');



//T ---> S '/') S
void T(void)


//S ---> F '^' S | F
void S(void)

enter('S'); F();
if (next == '^')
scan(); S();

leave('S');


//F ---> char | '(' E ')'
void F(void)

enter('F');
if (isalpha(next))

scan();

else if (next == '(')
scan(); E();
if (next == ')')
scan();
else
error(2);

else
error(3);

leave('F');

//Scan the entire input
void scan(void)
while (isspace(next = getchar()));


void error(int n)

printf("n*** ERROR: %in", n);
exit(1);


void enter(char name)

spaces(level++);
printf("+-%cn", name);


void leave(char name)

spaces(--level);
printf("+-%cn", name);


//TO display the parse tree
void spaces(int local_level)
");










share|improve this question















The following code is supposed to generate a parse tree of the input expression, but the problem is that the output E,T,F,S (functions used in the code). I want it to be something like:




a+b*c => E*c => E+b*c => a+b*c




 #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char next;
void E(void);void T(void);
void S(void);void F(void);
void error(int);void scan(void);
void enter(char);
void leave(char);
void spaces(int);
int level = 0;



//The main should always be very simple
//First scan the string
//second check for end of string reached , if yes success and if not error.

//P ---> E '#'
int main(void)
printf("Input:");
scan(); E();
if (next != '#') error(1);
else printf("***** Successful parse *****n");


//E ---> T '-') T
void E(void) next == '-')
scan();
T();

leave('E');



//T ---> S '/') S
void T(void)


//S ---> F '^' S | F
void S(void)

enter('S'); F();
if (next == '^')
scan(); S();

leave('S');


//F ---> char | '(' E ')'
void F(void)

enter('F');
if (isalpha(next))

scan();

else if (next == '(')
scan(); E();
if (next == ')')
scan();
else
error(2);

else
error(3);

leave('F');

//Scan the entire input
void scan(void)
while (isspace(next = getchar()));


void error(int n)

printf("n*** ERROR: %in", n);
exit(1);


void enter(char name)

spaces(level++);
printf("+-%cn", name);


void leave(char name)

spaces(--level);
printf("+-%cn", name);


//TO display the parse tree
void spaces(int local_level)
");







c++ c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 27 '13 at 22:26









Evan Mulawski

45.5k1099135




45.5k1099135










asked Jun 27 '13 at 21:55









Ar Khan

1114




1114











  • I am thoroughly confused. What do you mean by, "the problems is that the output E,T,F,S"? (I see nothing resembling that output when I run it on the same input.) How is a+b*c equivalent to E*c? Why do you end up with the same expression as you started with? How is the sequence you want related to the way the program generates output (which seems to be correct, btw)?
    – Marcelo Cantos
    Jun 28 '13 at 0:39











  • actually a+bc is just an example, and consider it as tree so ''precedence is high it will evaluate first and left part will be considered as expression i.e 'E' , similarly a+b and you of course you get the same expression in tree as in actual.
    – Ar Khan
    Jun 28 '13 at 2:34










  • the problem is that this code is giving output in the form of E,T,F,S , when it takes input expression then it should give out of same expression... :/ i am confused with that what changes should be made to do that... :)
    – Ar Khan
    Jun 28 '13 at 2:39
















  • I am thoroughly confused. What do you mean by, "the problems is that the output E,T,F,S"? (I see nothing resembling that output when I run it on the same input.) How is a+b*c equivalent to E*c? Why do you end up with the same expression as you started with? How is the sequence you want related to the way the program generates output (which seems to be correct, btw)?
    – Marcelo Cantos
    Jun 28 '13 at 0:39











  • actually a+bc is just an example, and consider it as tree so ''precedence is high it will evaluate first and left part will be considered as expression i.e 'E' , similarly a+b and you of course you get the same expression in tree as in actual.
    – Ar Khan
    Jun 28 '13 at 2:34










  • the problem is that this code is giving output in the form of E,T,F,S , when it takes input expression then it should give out of same expression... :/ i am confused with that what changes should be made to do that... :)
    – Ar Khan
    Jun 28 '13 at 2:39















I am thoroughly confused. What do you mean by, "the problems is that the output E,T,F,S"? (I see nothing resembling that output when I run it on the same input.) How is a+b*c equivalent to E*c? Why do you end up with the same expression as you started with? How is the sequence you want related to the way the program generates output (which seems to be correct, btw)?
– Marcelo Cantos
Jun 28 '13 at 0:39





I am thoroughly confused. What do you mean by, "the problems is that the output E,T,F,S"? (I see nothing resembling that output when I run it on the same input.) How is a+b*c equivalent to E*c? Why do you end up with the same expression as you started with? How is the sequence you want related to the way the program generates output (which seems to be correct, btw)?
– Marcelo Cantos
Jun 28 '13 at 0:39













actually a+bc is just an example, and consider it as tree so ''precedence is high it will evaluate first and left part will be considered as expression i.e 'E' , similarly a+b and you of course you get the same expression in tree as in actual.
– Ar Khan
Jun 28 '13 at 2:34




actually a+bc is just an example, and consider it as tree so ''precedence is high it will evaluate first and left part will be considered as expression i.e 'E' , similarly a+b and you of course you get the same expression in tree as in actual.
– Ar Khan
Jun 28 '13 at 2:34












the problem is that this code is giving output in the form of E,T,F,S , when it takes input expression then it should give out of same expression... :/ i am confused with that what changes should be made to do that... :)
– Ar Khan
Jun 28 '13 at 2:39




the problem is that this code is giving output in the form of E,T,F,S , when it takes input expression then it should give out of same expression... :/ i am confused with that what changes should be made to do that... :)
– Ar Khan
Jun 28 '13 at 2:39












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You don't get to choose the parse tree. I guess you don't understand the output, but (again) I guess you'd like



a+b*c => F+b*c => S+b*c => T+b*c => T+F*c => T+S*c => T+S*F => T+S*S => T+T => E



So here are a few questions to help.



  1. what kind of parsing is going on there? bottom up or top down?

  2. what state the parser is in when it prints enter/leave E, or T?

If you answered bottom up to the first question, what does E, T, S, F, F denotes (enter E, enter T, enter S, enter F, leave F)? When you have leave F that means the parser successfully recognised a non-terminal.
Try the input string 1+b*c. What do you get? Why do you get an error after E, T, S, F?



The output you seem to require can be easily produced if you understand what is produced at the moment. Hope this helps.






share|improve this answer



























    up vote
    0
    down vote













    Looks like a recursive descent parser. First, work out your grammar by hand. What you are expecting is not what your grammar says. You've got, from your comments,



    E ---> T '-') T expression
    T ---> S '/') S term
    S ---> F '^' S | F subexpression?
    F ---> char | '(' E ')' factor


    The definitions of E and T put * at a higher precedence than + so there is no way that you will get E*c. If you want that, you'll have to switch the grammar to



    E ---> T '/') T expression
    T ---> S ('+' term


    If you just want the output to include the rest of the expression,



    1. Get the whole line in

    2. Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.

    3. Change your Enter routine to print the the mnemonic as well as the line from the scanned point.





    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17353877%2fgenerate-parse-tree-in-c%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      You don't get to choose the parse tree. I guess you don't understand the output, but (again) I guess you'd like



      a+b*c => F+b*c => S+b*c => T+b*c => T+F*c => T+S*c => T+S*F => T+S*S => T+T => E



      So here are a few questions to help.



      1. what kind of parsing is going on there? bottom up or top down?

      2. what state the parser is in when it prints enter/leave E, or T?

      If you answered bottom up to the first question, what does E, T, S, F, F denotes (enter E, enter T, enter S, enter F, leave F)? When you have leave F that means the parser successfully recognised a non-terminal.
      Try the input string 1+b*c. What do you get? Why do you get an error after E, T, S, F?



      The output you seem to require can be easily produced if you understand what is produced at the moment. Hope this helps.






      share|improve this answer
























        up vote
        0
        down vote













        You don't get to choose the parse tree. I guess you don't understand the output, but (again) I guess you'd like



        a+b*c => F+b*c => S+b*c => T+b*c => T+F*c => T+S*c => T+S*F => T+S*S => T+T => E



        So here are a few questions to help.



        1. what kind of parsing is going on there? bottom up or top down?

        2. what state the parser is in when it prints enter/leave E, or T?

        If you answered bottom up to the first question, what does E, T, S, F, F denotes (enter E, enter T, enter S, enter F, leave F)? When you have leave F that means the parser successfully recognised a non-terminal.
        Try the input string 1+b*c. What do you get? Why do you get an error after E, T, S, F?



        The output you seem to require can be easily produced if you understand what is produced at the moment. Hope this helps.






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          You don't get to choose the parse tree. I guess you don't understand the output, but (again) I guess you'd like



          a+b*c => F+b*c => S+b*c => T+b*c => T+F*c => T+S*c => T+S*F => T+S*S => T+T => E



          So here are a few questions to help.



          1. what kind of parsing is going on there? bottom up or top down?

          2. what state the parser is in when it prints enter/leave E, or T?

          If you answered bottom up to the first question, what does E, T, S, F, F denotes (enter E, enter T, enter S, enter F, leave F)? When you have leave F that means the parser successfully recognised a non-terminal.
          Try the input string 1+b*c. What do you get? Why do you get an error after E, T, S, F?



          The output you seem to require can be easily produced if you understand what is produced at the moment. Hope this helps.






          share|improve this answer












          You don't get to choose the parse tree. I guess you don't understand the output, but (again) I guess you'd like



          a+b*c => F+b*c => S+b*c => T+b*c => T+F*c => T+S*c => T+S*F => T+S*S => T+T => E



          So here are a few questions to help.



          1. what kind of parsing is going on there? bottom up or top down?

          2. what state the parser is in when it prints enter/leave E, or T?

          If you answered bottom up to the first question, what does E, T, S, F, F denotes (enter E, enter T, enter S, enter F, leave F)? When you have leave F that means the parser successfully recognised a non-terminal.
          Try the input string 1+b*c. What do you get? Why do you get an error after E, T, S, F?



          The output you seem to require can be easily produced if you understand what is produced at the moment. Hope this helps.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jun 28 '13 at 3:00









          user1666959

          1,587811




          1,587811






















              up vote
              0
              down vote













              Looks like a recursive descent parser. First, work out your grammar by hand. What you are expecting is not what your grammar says. You've got, from your comments,



              E ---> T '-') T expression
              T ---> S '/') S term
              S ---> F '^' S | F subexpression?
              F ---> char | '(' E ')' factor


              The definitions of E and T put * at a higher precedence than + so there is no way that you will get E*c. If you want that, you'll have to switch the grammar to



              E ---> T '/') T expression
              T ---> S ('+' term


              If you just want the output to include the rest of the expression,



              1. Get the whole line in

              2. Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.

              3. Change your Enter routine to print the the mnemonic as well as the line from the scanned point.





              share|improve this answer
























                up vote
                0
                down vote













                Looks like a recursive descent parser. First, work out your grammar by hand. What you are expecting is not what your grammar says. You've got, from your comments,



                E ---> T '-') T expression
                T ---> S '/') S term
                S ---> F '^' S | F subexpression?
                F ---> char | '(' E ')' factor


                The definitions of E and T put * at a higher precedence than + so there is no way that you will get E*c. If you want that, you'll have to switch the grammar to



                E ---> T '/') T expression
                T ---> S ('+' term


                If you just want the output to include the rest of the expression,



                1. Get the whole line in

                2. Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.

                3. Change your Enter routine to print the the mnemonic as well as the line from the scanned point.





                share|improve this answer






















                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  Looks like a recursive descent parser. First, work out your grammar by hand. What you are expecting is not what your grammar says. You've got, from your comments,



                  E ---> T '-') T expression
                  T ---> S '/') S term
                  S ---> F '^' S | F subexpression?
                  F ---> char | '(' E ')' factor


                  The definitions of E and T put * at a higher precedence than + so there is no way that you will get E*c. If you want that, you'll have to switch the grammar to



                  E ---> T '/') T expression
                  T ---> S ('+' term


                  If you just want the output to include the rest of the expression,



                  1. Get the whole line in

                  2. Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.

                  3. Change your Enter routine to print the the mnemonic as well as the line from the scanned point.





                  share|improve this answer












                  Looks like a recursive descent parser. First, work out your grammar by hand. What you are expecting is not what your grammar says. You've got, from your comments,



                  E ---> T '-') T expression
                  T ---> S '/') S term
                  S ---> F '^' S | F subexpression?
                  F ---> char | '(' E ')' factor


                  The definitions of E and T put * at a higher precedence than + so there is no way that you will get E*c. If you want that, you'll have to switch the grammar to



                  E ---> T '/') T expression
                  T ---> S ('+' term


                  If you just want the output to include the rest of the expression,



                  1. Get the whole line in

                  2. Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.

                  3. Change your Enter routine to print the the mnemonic as well as the line from the scanned point.






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 28 '13 at 4:02









                  cup

                  4,6272925




                  4,6272925



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17353877%2fgenerate-parse-tree-in-c%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Barbados

                      How to read a connectionString WITH PROVIDER in .NET Core?

                      Node.js Script on GitHub Pages or Amazon S3