Skip to content

Instantly share code, notes, and snippets.

@jurgenvinju
Created March 27, 2017 08:18
Show Gist options
  • Save jurgenvinju/50eefa0c8fe140e2e912ad60aea97524 to your computer and use it in GitHub Desktop.
Save jurgenvinju/50eefa0c8fe140e2e912ad60aea97524 to your computer and use it in GitHub Desktop.
some functions for analyzing a grammar
module lang::rascal::grammar::analyze::Recursion
import Grammar;
import ParseTree;
import analysis::grammars::Dependency;
import Relation;
set[Symbol] reachable(type[&T] g, Symbol s) {
res = {s};
solve (res) {
res += {e | s <- res, /prod(/s, es, _) := g.definitions, e <- es};
}
return res;
}
@memo
set[Symbol] nullable(type[&T] g) {
res = {};
solve (res) {
res += {s | /prod(s, es, _) := g.definitions, {*es} <= res};
}
return res;
}
@memo
set[Production] recursive(type[&T] g) {
deps = symbolDependencies(grammar({}, g.definitions))+;
return {p | /p:prod(s,_,_) := g.definitions, <s,s> in deps};
}
@memo
rel[Production, int] rightRecursive(type[&T] g) {
deps = symbolDependencies(grammar({}, g.definitions))+;
nulls = nullable(g);
return { <p, size(l)> | /p:prod(_,[*l,s,*r],_) := g.definitions, <s,s> in deps, {*r} <= nulls};
}
@memo
rel[Production, int] leftRecursive(type[&T] g) {
deps = symbolDependencies(grammar({}, g.definitions))+;
nulls = nullable(g);
return {<p,size(l)> | /p:prod(_,[*l,s,*r],_) := g.definitions, <s,s> in deps, {*l} <= nulls};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment