0

I am still working on my simple language parser and I was wondering whether you could 'force' bison to parse from a certain line. By that I mean if there was some code such as:

int avar = 7
int bvar = 10
log avar
log bvar

I want bison to go and search through the lines and go to line 1 wherever it sees avar mentioned or line 2 wherever it says bvar mentioned. Obviously this is a very simple task with simple identifiers (and there are other ways to do this) but I was thinking whether it was possible for you to write a c function that can go to the line again. I would imagine that this would be useful in loops or user defined functions. Could you perhaps use another c function to go to the line by using the lineno variable from flex or is there an in built function. If you do have to use a c function, how would you go about telling bison to parse it again? Would it be by using the yyparse() and yylex() methods? I imagine that the answer would be very simple but I am quite new to bison, flex and c programming so any help would be appreciated. Thank you in advance.

First Edit:

This is not part of my actual language which is still in a very early simple stage. I was just wondering whether it would be possible in the future to do this if my language progresses into the future stage. A better example by what I mean would be in this loop:

int x = 0
begin loop 
x = x + 1
log x
end loop if x > 10

Would it be possible to reference yylex and yyparse for a certain line. For example the bison rule would be to do the statements and it would stop if the end if statement was true else it would go back to line 3 and it would continue doing that until the answer was correct. Would this be possible to implement?

  • first problem : the line 1 contains *avar* so you want to parse again the line 1 which obviously still contains *avar* so you want to parse again the line 1 which .... without ending. Second problem : if the lines are not in that order and *log avar* is before *int avar = 7* because you do not know where *int avar = 7* is (may be absent) how to 'jump' to that line and what about the bypassed lines ? – bruno Aug 28 '20 at 12:51
  • 1
    If think your question is not your real problem but a false problem created to try to solve the real one, for me when you parse (in order) a file containing your own language you need to memorize the read forms in a way to later be able to work on them. This is what a compiler / interpreter does – bruno Aug 28 '20 at 13:00
  • This is not part of my language. I was just wondering if it would be possible because I may need to do that to implement loops and user defined functions. – TheKonamiKoder Aug 28 '20 at 13:18
  • 1
    Bison doesn't read lines. **You** wrote a function called yylex which reads lines. So if you want the parser to read the same line again, you need to make it do that. – user253751 Aug 28 '20 at 13:34
  • 1
    @TheKonamiKoder I insist, you go in a wrong direction, you need to read and save all the file(s), *then* to execute or whatever you want. Note also a 'line' does not exist by itself in flex/bison, a line only exist if *you* have a special management about the newline – bruno Aug 28 '20 at 14:07

0 Answers0