Creating a lexer in C

I want to build a lexer in C, and I follow the book of dragons , I can understand state transitions, but how to implement them

Is there a better book?

The fact that I have to parse a string through several states so that I can determine if the string is acceptable or not!

+3
source share
5 answers

G'day

Assuming that you are referring to the Dragon book on compiler design, I would recommend looking at this page for compiler tools.

The page itself is rather small, but has links to various excellent resources on lexical analyzers.

NTN

amuses

+3

, , start- > part1- > part2- > end, switch , .

enum state { start=1, part1, part2, end} mystate;

// ...
mystate = start;
do {
  switch (mystate) {
    case start:
      // ...
    case part1:
      // ...
    case part2:
      // ...
      if (part2_end_condition) mystate = end; // state++ will also work
      // Note you could also set the state back to part1 on some condition here
      // which creates a loop
      break;
  }
} while (mystate != end);

, , /, :

var1    var2    var_end    next_state
0       0       0          state1
0       1       0          state2
1       0       0          state3
1       1       0          state4
-1      -1      1          state_end // -1 represents "doesn't matter" here
+6

. . , :

// regular expression
digit* [.digit*]

C :

// corresponding code
while(DIGIT(*pc)) pc++;
if (*pc=='.'){
    pc++;
    while(DIGIT(*pc)) pc++;
}

, , , , .

+3

, : Andrew W. Appel Maia Ginsburg, Modern C, Cambridge University Press, 2008.

2 : , , ; ;

+1

flex ( lex) .

lexer, C lexer .

, flex , C. , flex lexer...

0

Source: https://habr.com/ru/post/1710470/


All Articles