Lexical Analyzer

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 4
Reputation: techie2008 is an unknown quantity at this point 
Solved Threads: 0
techie2008 techie2008 is offline Offline
Newbie Poster

Lexical Analyzer

 
0
  #1
Aug 9th, 2008
guys, can u help me make a lexical analyzer that outputs invalid/valid only?
it would really help if you guys help me with this. this is my midterm project. thanks!!
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Lexical Analyzer

 
0
  #2
Aug 10th, 2008
Probably. Read the sticky posts...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Lexical Analyzer

 
0
  #3
Aug 10th, 2008
That would depend on the complexity of the "language" you were attempting to parse?

A C expression would probably be do-able.
A C program would not.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: techie2008 is an unknown quantity at this point 
Solved Threads: 0
techie2008 techie2008 is offline Offline
Newbie Poster

Re: Lexical Analyzer

 
0
  #4
Aug 10th, 2008
yes! it could be a C expression.. thanks in advance..
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Lexical Analyzer

 
0
  #5
Aug 10th, 2008
Well, let's remember C language (it's almost so called expression language) lexical grammar...
Here it is expression part:
C language expression tokens::

Identifiers:: all but sizeof

<letter|_>[<letter>|_|<digit>]...
----------------------------------

Operators and punctuators:: one of

[ ] ( )
:
? .
+ - * / % ^ & | ~
! = < > += -= *= /= %=
^= &= |= << >> >>= <<= == !=
<= >= && || ++ -- , ->
sizeof
-----------------

Literals:: one of

integer-literal
character-literal
floating-literal
string-literal
-----------------

Whitespaces:: one of

blanks
horizontal tab
vertical tab
newlines
formfeed
comment
-------
Add some punctuators (~5-6) and recognize some identifiers as keywords - and you have full C language scanner...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: techie2008 is an unknown quantity at this point 
Solved Threads: 0
techie2008 techie2008 is offline Offline
Newbie Poster

Re: Lexical Analyzer

 
0
  #6
Aug 11th, 2008
ahhhmmm..... would it be possible if you give me the source code?? i really can't get it.. i'm really stupid when it comes to this subject
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Lexical Analyzer

 
0
  #7
Aug 11th, 2008
This very minute? It's impossible. The scanner for C expressions is a program with ~1000 lines of source code (of course, it's a rough estimate). Probably, it's possible to write more compact (and fast) scanner with special methods (based on automata theory, for example).

Better start from a simple grammar. For example, consider simplest arithmetical expressions:

Tokens:: one of

identifier
decimal-integer-literal
Operators:: one of
+ - / *
Punctuators:: one of
( )

Try to implement a scanner for this lexical grammar.

Input: source text string (or char array)
Output: next token code
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Lexical Analyzer

 
0
  #8
Aug 11th, 2008
Given an input of say
10*(x + 2)

Something which prints
Found "10"
Found "*"
Found "("
Found "x"
Found "+"
Found "2"
Found ")"
would be a useful thing to accomplish.

When that works reliably, then think about adding handling for parentheses and operator precedence.

We're here to "help", not "give".
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Lexical Analyzer

 
0
  #9
Aug 11th, 2008
Follow Salem's advice!

I have wrote a scanner for simple C expressions (~300 LOCs) but I will post the source (may be ) only after you will try to do it yourself.

What's a bliss to write parsers for automat grammatics!..

Some remark: fortunately, a scanner does not know operator precedence (let syntax parser knows).
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 4
Reputation: techie2008 is an unknown quantity at this point 
Solved Threads: 0
techie2008 techie2008 is offline Offline
Newbie Poster

Re: Lexical Analyzer

 
0
  #10
Aug 12th, 2008
thanks for your help guys.. i have gotten through the basics, a friend helped me with it...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC