From cad14436abff83a5025a661349b97c4f90f91a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sylvain=20Hall=C3=A9?= Date: Wed, 5 Aug 2015 11:57:29 -0400 Subject: [PATCH] Fixed parsing of epsilon character in an input file. It turns out there are two different Unicode symbols for lowercase epsilon. --- .../src/ca/uqac/lif/bullwinkle/BnfParser.java | 2 +- .../src/ca/uqac/lif/bullwinkle/BnfRule.java | 3 +- .../ca/uqac/lif/bullwinkle/GrammarTests.java | 40 ++++++++++++++++++- build.xml | 1 + config.xml | 2 +- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfParser.java b/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfParser.java index 11c11a5..76e701f 100644 --- a/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfParser.java +++ b/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfParser.java @@ -307,7 +307,7 @@ public void setStartRule(final NonTerminalToken token) if (!alt_it.hasNext()) { // We succeeded in parsing the complete string: done - if (level > 0 || n_input.length() == 0) + if (level > 0 || (level == 0 && n_input.toString().trim().length() == 0)) { break; } diff --git a/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfRule.java b/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfRule.java index f621f42..1566479 100644 --- a/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfRule.java +++ b/Source/Parsing/src/ca/uqac/lif/bullwinkle/BnfRule.java @@ -96,8 +96,9 @@ public static BnfRule parseRule(String input) throws BnfRule.InvalidRuleExceptio Token to_add = new NonTerminalToken(trimmed_word); alternative_to_add.add(to_add); } - else if (trimmed_word.compareTo("ε") == 0) + else if (trimmed_word.compareTo("\uCEB5") == 0 || trimmed_word.compareTo("\u03B5") == 0) { + // There are two "lowercase epsilon" code points in Unicode; check for both Token to_add = new EpsilonTerminalToken(); alternative_to_add.add(to_add); } diff --git a/Source/ParsingTest/src/ca/uqac/lif/bullwinkle/GrammarTests.java b/Source/ParsingTest/src/ca/uqac/lif/bullwinkle/GrammarTests.java index ec55f58..d7a0654 100644 --- a/Source/ParsingTest/src/ca/uqac/lif/bullwinkle/GrammarTests.java +++ b/Source/ParsingTest/src/ca/uqac/lif/bullwinkle/GrammarTests.java @@ -77,7 +77,7 @@ public void parseGrammar2a() public void parseGrammar2b() { String expression = "SELECT a FROM (SELECT b FROM t)"; - ParseNode node = parseIt("data/Grammar-1.bnf", "", expression, true); + ParseNode node = parseIt("data/Grammar-1.bnf", "", expression, false); int size = node.getSize(); int expected_size = 19; if (size != expected_size) @@ -120,6 +120,13 @@ public void parseGrammar4() assertEquals("Incorrect parse tree size for expression " + expression, 5, node.getSize()); } + @Test + public void parseGrammar5() + { + String expression = "a WHERE"; + parseItNot("data/Grammar-10.bnf", "", expression, false); + } + @Test public void parseGrammarLtlFo1() { @@ -137,7 +144,7 @@ public void parseGrammarLtlFo1() public void parseGrammarWithEpsilon1() { String expression = "hello hello"; - ParseNode node = parseIt("data/Grammar-3.bnf", "", expression, false); + ParseNode node = parseIt("data/Grammar-3.bnf", "", expression, true); int size = node.getSize(); int expected_size = 6; if (size != expected_size) @@ -218,6 +225,13 @@ private ParseNode parseIt(String grammar_filename, String start_symbol, String e return node; } + private void parseItNot(String grammar_filename, String start_symbol, String expression, boolean debug_mode) + { + BnfParser parser = readGrammar(grammar_filename, start_symbol, debug_mode); + shouldNotParse(expression, parser); + } + + private ParseNode shouldParseAndNotNull(final String expression, final BnfParser parser) { ParseNode node = shouldParse(expression, parser); @@ -248,6 +262,28 @@ private ParseNode shouldParse(final String expression, final BnfParser parser) } return node; } + + /** + * Attempts to parse an expression with the given parser, and + * expects the parsing not to raise an exception or to return null. + * @param expression The expression to parse + * @param parser The parser to use + */ + private void shouldNotParse(final String expression, final BnfParser parser) + { + ParseNode node = null; + try + { + node = parser.parse(expression); + } + catch (BnfParser.ParseException e) + { + } + if (node != null) + { + fail("The parsing of " + expression + " should have failed"); + } + } public static BnfParser readGrammar(final String filename, final String start_rule, boolean debug_mode) { diff --git a/build.xml b/build.xml index e637b84..011669d 100644 --- a/build.xml +++ b/build.xml @@ -158,6 +158,7 @@ diff --git a/config.xml b/config.xml index 5369505..255ecb8 100644 --- a/config.xml +++ b/config.xml @@ -26,7 +26,7 @@ Bullwinkle - 1.1.3 + 1.1.4