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)
");
c++ c
add a comment |
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)
");
c++ c
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 isa+b*c
equivalent toE*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
add a comment |
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)
");
c++ c
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
c++ c
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 isa+b*c
equivalent toE*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
add a comment |
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 isa+b*c
equivalent toE*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
add a comment |
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.
- what kind of parsing is going on there? bottom up or top down?
- what state the parser is in when it prints enter/leave
E
, orT
?
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.
add a comment |
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,
- Get the whole line in
- Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.
- Change your Enter routine to print the the mnemonic as well as the line from the scanned point.
add a comment |
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.
- what kind of parsing is going on there? bottom up or top down?
- what state the parser is in when it prints enter/leave
E
, orT
?
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.
add a comment |
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.
- what kind of parsing is going on there? bottom up or top down?
- what state the parser is in when it prints enter/leave
E
, orT
?
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.
add a comment |
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.
- what kind of parsing is going on there? bottom up or top down?
- what state the parser is in when it prints enter/leave
E
, orT
?
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.
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.
- what kind of parsing is going on there? bottom up or top down?
- what state the parser is in when it prints enter/leave
E
, orT
?
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.
answered Jun 28 '13 at 3:00
user1666959
1,587811
1,587811
add a comment |
add a comment |
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,
- Get the whole line in
- Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.
- Change your Enter routine to print the the mnemonic as well as the line from the scanned point.
add a comment |
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,
- Get the whole line in
- Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.
- Change your Enter routine to print the the mnemonic as well as the line from the scanned point.
add a comment |
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,
- Get the whole line in
- Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.
- Change your Enter routine to print the the mnemonic as well as the line from the scanned point.
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,
- Get the whole line in
- Change your scanner or lexer to get the next character from that scanned line. Mark this as the scanned point.
- Change your Enter routine to print the the mnemonic as well as the line from the scanned point.
answered Jun 28 '13 at 4:02
cup
4,6272925
4,6272925
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 toE*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