aboutsummaryrefslogtreecommitdiffstats
path: root/Llk
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2016-02-22 10:23:37 +0100
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2016-07-15 16:50:21 +0200
commit0b598a6eb9e3a38e4c2b60fe8c45679ac43fa80b (patch)
treecad7e64fbf18fb74deac3d50b1301a0149b454fe /Llk
parent8f47ccde0009537ae6c7ac24fc8cf3db717ef6ce (diff)
downloadCompiler-0b598a6eb9e3a38e4c2b60fe8c45679ac43fa80b.zip
Compiler-0b598a6eb9e3a38e4c2b60fe8c45679ac43fa80b.tar.gz
Compiler-0b598a6eb9e3a38e4c2b60fe8c45679ac43fa80b.tar.bz2
Llk: Add the `parser.lookahead` pragma.
`parser.lookahead` is used by the parser to define the `k` in LL(k), i.e. number of tokens to lookahead. By default, it is set to 1024. Memory can be reduced by using an appropriated value.
Diffstat (limited to 'Llk')
-rw-r--r--Llk/Llk.php11
-rw-r--r--Llk/Parser.php8
2 files changed, 16 insertions, 3 deletions
diff --git a/Llk/Llk.php b/Llk/Llk.php
index c64fad2..e09835e 100644
--- a/Llk/Llk.php
+++ b/Llk/Llk.php
@@ -116,7 +116,10 @@ class Llk
* declare a pragma. Currently support pragmas are:
*
* * `lexer.unicode`, used by the lexer to turn the Unicode mode on for
- * the regular expressions.
+ * the regular expressions,
+ * * `parser.lookahead`, used by the parser to define the `k` in LL(k),
+ * i.e. number of tokens to lookahead. By default, it is set to 1024.
+ * Memory can be reduced by using an appropriated value.
*
* @param \Hoa\Stream\IStream\In $stream Stream that contains the
* grammar.
@@ -326,7 +329,11 @@ class Llk
break;
default:
- $pragmaValue = $matches[2];
+ if (true === ctype_digit($matches[2])) {
+ $pragmaValue = intval($matches[2]);
+ } else {
+ $pragmaValue = $matches[2];
+ }
}
$pragmas[$matches[1]] = $pragmaValue;
diff --git a/Llk/Parser.php b/Llk/Parser.php
index 23bdd0e..e1204f3 100644
--- a/Llk/Parser.php
+++ b/Llk/Parser.php
@@ -153,10 +153,16 @@ class Parser
*/
public function parse($text, $rule = null, $tree = true)
{
+ $k = 1024;
+
+ if (isset($this->_pragmas['parser.lookahead'])) {
+ $k = max(0, intval($this->_pragmas['parser.lookahead']));
+ }
+
$lexer = new Lexer($this->_pragmas);
$this->_tokenSequence = new Iterator\Buffer(
$lexer->lexMe($text, $this->_tokens),
- 1024
+ $k
);
$this->_tokenSequence->rewind();