Link to home
Start Free TrialLog in
Avatar of drnick
drnick

asked on

C Parser 1 step question

Hi @all,

today's evening i'll have to start writing a c parser in java.
i'll be nothing spectacular but a small one.
i've done this before, so there's no big problem, except the following:
in previous versions, there was no thing like "parsing error" in
concern of forgotten ")" or "}" or faulty use of functions and such.
the parser then just replied "error - try again".
however, this is to change.
i want to be able to output the line numbers where errors occured
and highlight the faulty statements in that lines.
well, up to now i just made one big string of all lines and then parsed it.
now i can't do that anymore, because i'll loose line numbers.
i get the code as a vector of strings.
my question is: how can i dismantel this vector in a vector of
code ranges, all representing a "{...}" or such in the cleverest way.
i don't want special stuff like string awareness or {-depth couting.
the thing of interest is just, how to handle the input in a way that allows
me to continuous read characters like i was going through a string while
still preserving line numbers.
or maybe you'll have a whole other approach.
however, since i'll start working on this in approximately 9 hours from now,
it would be nice if you post your comments up to then.
after that time i'll have to do the stuff by my self and postet comments
will not be concerned.
thanks, drnick
Avatar of drnick
drnick

ASKER

oh, i forgot, you don't need to post java code,
code of any language is ok except grammar parser like yacc or such.
Avatar of sunnycoder
Hi drnick,

> i want to be able to output the line numbers where errors occured
> and highlight the faulty statements in that lines.
> well, up to now i just made one big string of all lines and then parsed it.
> now i can't do that anymore, because i'll loose line numbers.
> i get the code as a vector of strings.

Normal approach is to maintain global variables which hold the current line number, function name and filename ...

Since you are interested only in line number, you can maintain only one global variable ...

Skip the step of combining the entire input in one big string ... Rather loop over it line by line ... something like

while ( more data to read from file )
{
       read one line;
       increment line number;
       process the line;
       if Error
             process error and give line number
}

Since lexical scan is linear, I believe there should be no problem with this approach ...

Sunnycoder
ASKER CERTIFIED SOLUTION
Avatar of cookre
cookre
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would transform the vector of strings into a vector of tokens, where each token was something like this (I haven't created constructors, or marked the members public or private):

class Token {
    String token;
    FileLocation location;
    // and whatever other flags you want to include
}

class FileLocation {
     int firstLine, lastLine, firstColumn, lastColumn;
     String filename; // or maybe "File sourceFile;"
}

You could re-use the FileLocation in later data structures (like your parse tree) so that you can display errors during execution/compilation/whatever.
Avatar of drnick

ASKER

hello again,
thanks for your quick answers.
i decided to accept cookre's one,
because it is the nearest to my big-string-thing i'd done before.
however, it is as same gracefully as sunnycoders idea.
so i would like to accept his answer as an assistent answer,
how to do that? i can't see no button fo that?
NovaDenizen, you come close to what i thought of first,
but there i got the problem between continuous string reading vs. line numbering.
i wanted to put my text in such a class, but the problem to parse it continuously is still valid
for that construct.
however, thanks for your support.
drnick
Post a question at Customer Support:
https://www.experts-exchange.com/Community_Support/

Tell them what you intended to do and they can adjust the grading for you - I've no problem with it.
Avatar of drnick

ASKER

hm, i got another idea,
i'll some additional points for sunny coder under

https://www.experts-exchange.com/questions/20974968/Points-for-sunnycoder.html