Changeset 290

Show
Ignore:
Timestamp:
05/19/08 22:00:34 (8 months ago)
Author:
chris
Message:

Fix infinite loop caused by recursive rules.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lex/trunk/src/com/qwirx/lex/parser/Edge.java

    r125 r290  
    4141    boolean isAt(int pos); 
    4242    boolean includes(Edge other); 
     43    boolean uses(Rule rule); 
    4344    TreeNode toTree(); 
    4445    String getHtmlLabel(); 
  • lex/trunk/src/com/qwirx/lex/parser/MorphEdge.java

    r259 r290  
    146146    } 
    147147     
     148    public boolean uses(Rule rule) 
     149    { 
     150        return false; 
     151    } 
     152     
    148153    public String getHtmlLabel() 
    149154    { 
  • lex/trunk/src/com/qwirx/lex/parser/Parser.java

    r142 r290  
    9090                } 
    9191                                */ 
     92                 
     93                // Don't re-use a rule that's already been used 
     94                // somewhere in this edge. That could lead to 
     95                // infinite recursion and should not be necessary. 
    9296 
    9397                Rule rule = rules[r]; 
    94                 List newEdges = rule.applyTo(chart, i); 
    95                 inputCopy.addAll(i + 1, newEdges); 
     98 
     99                if (!inputEdge.uses(rule)) 
     100                { 
     101                    List newEdges = rule.applyTo(chart, i); 
     102                    inputCopy.addAll(i + 1, newEdges); 
     103                }                 
    96104                        } 
    97105                } 
  • lex/trunk/src/com/qwirx/lex/parser/Rule.java

    r120 r290  
    688688    } 
    689689 
    690     private void continueParseWithNextPart(List newEdges) 
     690    private void continueParseWithNextPart(List<Edge> newEdges) 
    691691    { 
    692692        boolean foundUnfinishedPart = false; 
     
    719719        // and we should be ready to create a new edge. 
    720720         
    721         // don't create new edges which don't contain any other edges: 
    722         // we could keep doing that forever. 
     721        // don't create new edges which don't contain any other edges 
     722        // (all parts optional and skipped): we could keep doing that forever. 
    723723         
    724724        int numEdges = 0; 
  • lex/trunk/src/com/qwirx/lex/parser/RuleEdge.java

    r121 r290  
    459459        return false; 
    460460    } 
    461      
     461 
     462    public boolean uses(Rule rule) 
     463    { 
     464        if (this.rule == rule) 
     465        { 
     466            return true; 
     467        } 
     468         
     469        for (int i = 0; i < parts.length; i++) 
     470        { 
     471            if (parts[i].uses(rule)) 
     472            { 
     473                return true; 
     474            } 
     475        } 
     476         
     477        return false; 
     478    } 
     479     
     480 
    462481    public void getLeavesInto(List leaves) 
    463482    { 
  • lex/trunk/src/com/qwirx/lex/parser/WordEdge.java

    r123 r290  
    139139    } 
    140140     
     141    public boolean uses(Rule rule) 
     142    { 
     143        return false; 
     144    } 
     145     
    141146    public String getHtmlLabel() 
    142147    { 
  • lex/trunk/test/com/qwirx/lex/ParserTest.java

    r131 r290  
    20062006    } 
    20072007 
     2008    public void testInfiniteLoop() throws Exception  
     2009    { 
     2010        Rule[] rules = new Rule[]{ 
     2011                Rule.makeFromString2(1, "C", "{B}"), 
     2012                Rule.makeFromString2(2, "B", "x"), 
     2013                Rule.makeFromString2(3, "B", "{A}"), 
     2014                Rule.makeFromString2(4, "A", "{B}"), 
     2015        }; 
     2016         
     2017        Parser p = new Parser(rules); 
     2018        p.setVerbose(true); 
     2019 
     2020        List results = p.parseFor("x", "C"); 
     2021        assertEquals(2, results.size()); 
     2022 
     2023        Edge C = (Edge)(results.get(1)); 
     2024        assertEquals("C", C.symbol()); 
     2025        assertEquals("B", C.part(0).symbol()); 
     2026        assertEquals("x", C.part(0).part(0).symbol()); 
     2027 
     2028        C = (Edge)(results.get(0)); 
     2029        assertEquals("C", C.symbol()); 
     2030        assertEquals("B", C.part(0).symbol()); 
     2031        assertEquals("A", C.part(0).part(0).symbol()); 
     2032        assertEquals("B", C.part(0).part(0).part(0).symbol()); 
     2033        assertEquals("x", C.part(0).part(0).part(0).part(0).symbol()); 
     2034    } 
     2035 
    20082036        public static void main(String[] args)  
    20092037    {