Parse EBNF: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 16:
<br>
A more or less faithful translation except that indices are 0-based rather than 1-based and so 1 less than in the Phix results.
<langsyntaxhighlight lang="go">package main
 
import (
Line 420:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 503:
We use Parsec to generate Parsec.
 
<langsyntaxhighlight lang="haskell">import Control.Applicative
import Control.Monad
import Data.Maybe
Line 675:
lc c = char c <* ws
 
ws = many $ oneOf " \n\t"</langsyntaxhighlight>
 
=={{header|Julia}}==
Line 683:
Tested with Julia v1.7.2
 
<langsyntaxhighlight lang="julia">
struct Grammar
regex::Regex
Line 835:
end
 
</syntaxhighlight>
</lang>
 
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE EBNF;
 
FROM ASCII IMPORT EOL;
Line 927:
Tabulate (T0);
Tabulate (T1);
END EBNF.</langsyntaxhighlight>
And the source for the EBNF scanner. I hope you like nested procedures.
<langsyntaxhighlight lang="modula2">IMPLEMENTATION MODULE EBNFScanner;
 
FROM ASCII IMPORT LF;
Line 1,096:
Ino := 0;
ch := ' '
END EBNFScanner.</langsyntaxhighlight>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # http://www.rosettacode.org/wiki/Parse_EBNF
Line 1,273:
----------------------------------------------------------------------
{ foo = bar . } "undefined production check"
----------------------------------------------------------------------</langsyntaxhighlight>
{{out}}
<pre>
Line 1,387:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">src</span>
Line 1,704:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
In real use, I would be tempted to use numeric literals rather than string tags in such structures, but the latter certainly make things ten times easier to debug, plus I got an instantly legible syntax tree dump (the bit just after "===>" below) practically for free.
{{out}}
Line 1,771:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de EBNF
"expr : term ( ( PLUS | MINUS ) term )* ;"
"term : factor ( ( MULT | DIV ) factor )* ;"
Line 1,780:
(unless (and (match '(@S : @E ;) (str E)) (not (cdr @S)))
(quit "Invalid EBNF" E) )
(put (car @S) 'ebnf @E) ) )</langsyntaxhighlight>
<langsyntaxhighlight PicoLisplang="picolisp">(de matchEbnf (Pat)
(cond
((asoq Pat '((PLUS . +) (MINUS . -) (MULT . *) (DIV . /)))
Line 1,823:
(let *Lst (str Str "")
(catch NIL
(parseLst (get 'expr 'ebnf)) ) ) )</langsyntaxhighlight>
Output:
<pre>: (parseEbnf "1 + 2 * -3 / 7 - 3 * 4")
Line 1,835:
It is implemented and exercised using the flavor of EBNF and test cases specified on the [[Parse EBNF/Tests|test page]].
 
<syntaxhighlight lang="raku" line>
<lang perl6>
# A Raku grammar to parse EBNF
grammar EBNF {
Line 1,973:
unlink $fn;
}
</syntaxhighlight>
</lang>
 
Output:
Line 2,244:
{{in progress|lang=Ruby|day=12|month=May|year=2011}}
{{incomplete|Ruby|The tokenizer is here, but the parser is very incomplete.}}
<langsyntaxhighlight lang="ruby">#--
# The tokenizer splits the input into Tokens like "identifier",
# ":", ")*" and so on. This design uses a StringScanner on each line of
Line 2,382:
parse
end
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
Line 2,388:
 
Demonstration lexer and parser. Note that this parser supports parenthesized expressions, making the grammar recursive.
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
# Utilities to make the coroutine easier to use
Line 2,522:
}
throw SYNTAX "\"$payload\" at position $index"
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="tcl"># Demonstration code
puts [parse "1 - 2 - -3 * 4 + 5"]
puts [parse "1 - 2 - -3 * (4 + 5)"]</langsyntaxhighlight>
Output:
<pre>
Line 2,540:
{{libheader|Wren-seq}}
Translated via the Go entry.
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Conv, Fmt
import "/trait" for Stepped
import "/str" for Char
Line 2,907:
System.print()
i = i + 1
}</langsyntaxhighlight>
 
{{out}}
10,333

edits