Note that there are some explanatory texts on larger screens.

plurals
  1. POTranslate one term differently in one program
    primarykey
    data
    text
    <p>I try to make a frontend for a kind of programs... there are 2 particularities:</p> <p>1) When we meet a string beginning with <code>=</code>, I want to read the rest of the string as a <code>formula</code> instead of a string value. For instance, <code>"123"</code>, <code>"TRUE"</code>, <code>"TRUE+123"</code> are considered having <code>string</code> as type, while <code>"=123"</code>, <code>"=TRUE"</code>, <code>"=TRUE+123"</code> are considered having <code>Syntax.formula</code> as type. By the way,</p> <pre><code>(* in syntax.ml *) and expression = | E_formula of formula | E_string of string ... and formula = | F_int of int | F_bool of bool | F_Plus of formula * formula | F_RC of rc and rc = | RC of int * int </code></pre> <p>2) Inside the formula, some strings are interpreted differently from outside. For instance, in a command <code>R4C5 := 4</code>, <code>R4C5</code> which is actually a variable, is considered as a <code>identifier</code>, while in <code>"=123+R4C5"</code> which tries to be translated to a formula, <code>R4C5</code> is translated as <code>RC (4,5): rc</code>.</p> <p>So I don't know how to realize this with 1 or 2 lexers, and 1 or 2 parsers. </p> <p>At the moment, I try to realize all in 1 lexer and 1 parser. Here is part of code, which doesn't work, it still considers <code>R4C5</code> as <code>identifier</code>, instead of <code>rc</code>:</p> <pre><code>(* in lexer.mll *) let begin_formula = double_quote "=" let end_formula = double_quote let STRING = double_quote ([^ "=" ])* double_quote rule token = parse ... | begin_formula { BEGIN_FORMULA } | 'R' { R } | 'C' { C } | end_formula { END_FORMULA } | lex_identifier as li { try Hashtbl.find keyword_table (lowercase li) with Not_found -&gt; IDENTIFIER li } | STRING as s { STRING s } ... (* in parser.mly *) expression: | BEGIN_FORMULA f = formula END_FORMULA { E_formula f } | s = STRING { E_string s } ... formula: | i = INTEGER { F_int i } | b = BOOL { F_bool b } | f0 = formula PLUS f1 = formula { F_Plus (f0, f1) } | rc { F_RC $1 } rc: | R i0 = INTEGER C i1 = INTEGER { RC (i0, i1) } </code></pre> <p>Could anyone help?</p> <p><strong>New idea:</strong> I am thinking of sticking on 1 lexer + 1 parser, and create a <em>entrypoint</em> for formula in lexer as what we do normally for <code>comment</code>... here are some updates in <code>lexer.mll</code> and <code>parser.mly</code>:</p> <pre><code>(* in lexer.mll *) rule token = parse ... | begin_formula { formula lexbuf } ... | INTEGER as i { INTEGER (int_of_string i) } | '+' { PLUS } ... and formula = parse | end_formula { token lexbuf } | INTEGER as i { INTEGER_F (int_of_string i) } | 'R' { R } | 'C' { C } | '+' { PLUS_F } | _ { raise (Lexing_error ("unknown in formula")) } (* in parser.mly *) expression: | formula { E_formula f } ... formula: | i = INTEGER_F { F_int i } | f0 = formula PLUS_F f1 = formula { F_Plus (f0, f1) } ... </code></pre> <p>I have done some tests, for instance to parse <code>"=R4"</code>, the problem is that it can parse well <code>R</code>, but it considers <code>4</code> as <code>INTEGER</code> instead of <code>INTEGER_F</code>, it seems that <code>formula lexbuf</code> needs to be added from time to time in the body of <code>formula</code> entrypoint (Though I don't understand why parsing in the body of <code>token</code> entrypoint works without always mentioning <code>token lexbuf</code>). I have tried several possibilities: <code>| 'R' { R; formula lexbuf }</code>, <code>| 'R' { formula lexbuf; R }</code>, etc. but it didn't work... ... Could anyone help?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload