Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
12-01-2010, 02:29 PM
#16
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
MTK358
Do you mean macros?
And is there anything wrong with FOLLOWED_BY being a function, or are you just saying it doesn't have to be?
Yes, I mean macros.
I also suggested to "C" GNU dialect - for a reason. Here is a code sample:
Code:
sergei@amdam2:~/junk/gnu_code_blocks> cat -n main.c
1 #include <stdio.h>
2
3 int main()
4 {
5 char stream[1000] = "step";
6 unsigned stream_position = 0;
7
8 1 // trivial truth
9 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 's') {stream_position++; result = 1;} fprintf(stderr, "'s' matches\n"); result;})
10 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 't') {stream_position++; result = 1;} fprintf(stderr, "'t' matches\n"); result;})
11 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 'e') {stream_position++; result = 1;} fprintf(stderr, "'e' matches\n"); result;})
12 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 'p') {stream_position++; result = 1;} fprintf(stderr, "'p' matches\n"); result;});
13
14 return 0;
15 }
sergei@amdam2:~/junk/gnu_code_blocks> ~/AFSWD/install/gcc-4.4.5/binsh/gcc main.c -o funny_parser
sergei@amdam2:~/junk/gnu_code_blocks> ./funny_parser
's' matches
't' matches
'e' matches
'p' matches
sergei@amdam2:~/junk/gnu_code_blocks>
- you see,
stackable pieces of code, which can be easily converted into macro.
12-01-2010, 02:32 PM
#17
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
MTK358
I'm also starting to think that, and that's what Sergei seems to be leading to.
Sergei is leading towards recursion part after the care is taken of non-recursion part.
12-01-2010, 02:54 PM
#18
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Original Poster
Quote:
Originally Posted by
Sergei Steshenko
Yes, I mean macros.
I also suggested to "C" GNU dialect - for a reason. Here is a code sample:
Code:
sergei@amdam2:~/junk/gnu_code_blocks> cat -n main.c
1 #include <stdio.h>
2
3 int main()
4 {
5 char stream[1000] = "step";
6 unsigned stream_position = 0;
7
8 1 // trivial truth
9 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 's') {stream_position++; result = 1;} fprintf(stderr, "'s' matches\n"); result;})
10 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 't') {stream_position++; result = 1;} fprintf(stderr, "'t' matches\n"); result;})
11 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 'e') {stream_position++; result = 1;} fprintf(stderr, "'e' matches\n"); result;})
12 && ({unsigned result = 0; unsigned saved_position = stream_position;stream_position; if(stream[stream_position] == 'p') {stream_position++; result = 1;} fprintf(stderr, "'p' matches\n"); result;});
13
14 return 0;
15 }
What's the advantage of that, seems like using a macro or function would be much less repetetive.
12-01-2010, 03:00 PM
#19
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
MTK358
What's the advantage of that, seems like using a macro or function would be much less repetetive.
As I wrote earlier, first copy-paste in order to understand what can/should be abstracted out/hidden in a macro.
The code I gave shows some important thoughts, for example, 'fprintf' statements are for debugging/tracing; the place where 'result=1' is also a place into which useful code (e.g. adding items to AST) can be added - the code will be execute upon language construct match.
In my
http://www.linuxquestions.org/questi...ml#post4177393 post it is prominently written:
Quote:
stackable pieces of code, which can be easily converted into macro.
Last edited by Sergei Steshenko; 12-01-2010 at 03:02 PM .
12-01-2010, 03:10 PM
#20
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Original Poster
Also, I don't get this:
Code:
unsigned saved_position = stream_position;stream_position;
What's the reason for "stream_position;", it looks like it does nothing.
12-01-2010, 03:32 PM
#21
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
MTK358
Also, I don't get this:
Code:
unsigned saved_position = stream_position;stream_position;
What's the reason for "stream_position;", it looks like it does nothing.
A typo - you can safely remove it.
12-01-2010, 03:37 PM
#22
Senior Member
Registered: May 2005
Posts: 4,481
By the way, 'fprintf' is in a wrong place too, the code should be:
Code:
sergei@amdam2:~/junk/gnu_code_blocks> cat -n main.c
1 #include <stdio.h>
2
3 int main()
4 {
5 char stream[1000] = "step";
6 unsigned stream_position = 0;
7
8 1 // trivial truth
9 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 's') {stream_position++; result = 1; fprintf(stderr, "'s' matches\n");} result;})
10 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 't') {stream_position++; result = 1; fprintf(stderr, "'t' matches\n");} result;})
11 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 'e') {stream_position++; result = 1; fprintf(stderr, "'e' matches\n");} result;})
12 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 'p') {stream_position++; result = 1; fprintf(stderr, "'p' matches\n");} result;});
13
14 return 0;
15 }
sergei@amdam2:~/junk/gnu_code_blocks>
.
12-01-2010, 04:04 PM
#23
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Original Poster
So that big expression will evalute to true if the input stream at the current point matches "step", otherwise it will return 0 and leave the stream position at the first unsuccessful character, right?
Also, what's the reason for the leading "1 &&"?
12-01-2010, 04:25 PM
#24
Senior Member
Registered: May 2005
Posts: 4,481
Quote:
Originally Posted by
MTK358
So that big expression will evalute to true if the input stream at the current point matches "step", otherwise it will return 0 and leave the stream position at the first unsuccessful character, right?
Also, what's the reason for the leading "1 &&"?
Not right. The position is advanced with each
partial match.
So, if you are to match the whole "step", you need to wrap the code:
Code:
sergei@amdam2:~/junk/gnu_code_blocks> cat -n main.c
1 #include <stdio.h>
2
3 int main()
4 {
5 char stream[1000] = "step";
6 unsigned stream_position = 0;
7
8 1 // trivial truth
9 && (
10 {
11 unsigned result = 0;
12 unsigned saved_position = stream_position;
13 if(
14 1
15 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 's') {stream_position++; result = 1; fprintf(stderr, "'s' matches\n");} result;})
16 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 't') {stream_position++; result = 1; fprintf(stderr, "'t' matches\n");} result;})
17 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 'e') {stream_position++; result = 1; fprintf(stderr, "'e' matches\n");} result;})
18 && ({unsigned result = 0; unsigned saved_position = stream_position; if(stream[stream_position] == 'p') {stream_position++; result = 1; fprintf(stderr, "'p' matches\n");} result;})
19 )
20 {
21 result = 1;
22 {
23 fprintf(stderr,"the matching part: ");
24
25 unsigned pos;
26 for(pos = saved_position; pos < stream_position; pos++)
27 {
28 fprintf(stderr, "%c", stream[pos]);
29 }
30
31 fprintf(stderr,"\n");
32 }
33 }
34 result;
35 }
36 );
37 return 0;
38 }
sergei@amdam2:~/junk/gnu_code_blocks> ~/AFSWD/install/gcc-4.4.5/binsh/gcc -Wall -Wextra main.c -o funny_parser
main.c: In function ‘main’:
main.c:15: warning: unused variable ‘saved_position’
main.c:16: warning: unused variable ‘saved_position’
main.c:17: warning: unused variable ‘saved_position’
main.c:18: warning: unused variable ‘saved_position’
main.c:8: warning: value computed is not used
sergei@amdam2:~/junk/gnu_code_blocks> ./funny_parser
's' matches
't' matches
'e' matches
'p' matches
the matching part: step
sergei@amdam2:~/junk/gnu_code_blocks>
The
part is for convenience, i.e. a parser can be formulated as:
Code:
#define TRIVIAL_TRUTH 1
...
TRIVIAL_TRUTH
FOLLOWED_BY(...)
FOLLOWED_BY(...)
...
.
And if backtracking is to be implemented, the outer 'if' used to check matching of the whole 'step' should be accompanied by
Code:
else
{
stream_position = saved_position;
}
, i.e. for cases of lazy writing - say, you first try to match 'step', and then from scratch, when it fails, you want to match 'stop'.
By the way, a good illustration of usefulness of scoping rules - variable names are reused everywhere, and no collision occurs.
All times are GMT -5. The time now is 01:19 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News