LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 12-01-2010, 02:29 PM   #16
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454

Quote:
Originally Posted by MTK358 View Post
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.
 
Old 12-01-2010, 02:32 PM   #17
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 12-01-2010, 02:54 PM   #18
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 12-01-2010, 03:00 PM   #19
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 12-01-2010, 03:10 PM   #20
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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.
 
Old 12-01-2010, 03:32 PM   #21
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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.
 
Old 12-01-2010, 03:37 PM   #22
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
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>
.
 
Old 12-01-2010, 04:04 PM   #23
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
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 &&"?
 
Old 12-01-2010, 04:25 PM   #24
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by MTK358 View Post
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

Code:
1 &&
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
'nm' lists all symbols - including static library symbols painulyarun Programming 4 04-06-2010 04:04 AM
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
Debugger selectively not using symbols in library with symbols Millenniumman Programming 5 03-25-2007 09:44 AM
conflicting types error in parser.c snecklifter Programming 6 04-08-2005 06:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration