corinthia-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pmke...@apache.org
Subject incubator-corinthia git commit: Flat: Resolve references after building grammar
Date Tue, 14 Jul 2015 10:58:33 GMT
Repository: incubator-corinthia
Updated Branches:
  refs/heads/master e0b656433 -> 9dcccd197


Flat: Resolve references after building grammar

For any Ident expressions, resolve them by looking up the corresponding
rule in the grammar and setting the first child expression to the
expression of the target rule.


Project: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/commit/9dcccd19
Tree: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/tree/9dcccd19
Diff: http://git-wip-us.apache.org/repos/asf/incubator-corinthia/diff/9dcccd19

Branch: refs/heads/master
Commit: 9dcccd1976163d53908abd39829ee7bed8805648
Parents: e0b6564
Author: Peter Kelly <peter@uxproductivity.com>
Authored: Sun Jul 12 23:35:24 2015 +0700
Committer: Peter Kelly <peter@uxproductivity.com>
Committed: Sun Jul 12 23:35:24 2015 +0700

----------------------------------------------------------------------
 experiments/flat/src/Builtin.c    |  6 ++++--
 experiments/flat/src/Expression.c | 33 +++++++++++++++++++++++++++++----
 experiments/flat/src/Expression.h |  4 +++-
 experiments/flat/src/Grammar.c    | 32 ++++++++++++++++++++++++++++++++
 experiments/flat/src/Grammar.h    |  1 +
 5 files changed, 69 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/9dcccd19/experiments/flat/src/Builtin.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Builtin.c b/experiments/flat/src/Builtin.c
index 3bcc475..a5d3923 100644
--- a/experiments/flat/src/Builtin.c
+++ b/experiments/flat/src/Builtin.c
@@ -19,7 +19,7 @@
 #include <stdarg.h>
 #include <stdlib.h>
 
-#define ref(name)       ExpressionNewValue(IdentExpr,name)
+#define ref(name)       ExpressionNewIdent(name)
 #define seq(...)        makeExpression(SequenceExpr,__VA_ARGS__,NULL)
 #define choice(...)     makeExpression(ChoiceExpr,__VA_ARGS__,NULL)
 #define and(...)        makeExpression(AndExpr,__VA_ARGS__,NULL)
@@ -27,7 +27,7 @@
 #define opt(...)        makeExpression(OptExpr,__VA_ARGS__,NULL)
 #define star(...)       makeExpression(StarExpr,__VA_ARGS__,NULL)
 #define plus(...)       makeExpression(PlusExpr,__VA_ARGS__,NULL)
-#define lit(value)      ExpressionNewValue(LitExpr,value)
+#define lit(value)      ExpressionNewLit(value)
 #define cls(...)        makeExpression(ClassExpr,__VA_ARGS__,NULL)
 #define dot()           makeExpression(DotExpr,NULL)
 #define range(lo,hi)    ExpressionNewRange(lo,hi)
@@ -222,5 +222,7 @@ Grammar *GrammarNewBuiltin(void)
     // EndOfFile  <- !.
     GrammarDefine(gram,"EndOfFile",not(dot()));
 
+    GrammarResolve(gram);
+
     return gram;
 }

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/9dcccd19/experiments/flat/src/Expression.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Expression.c b/experiments/flat/src/Expression.c
index 7bd7368..436a9cd 100644
--- a/experiments/flat/src/Expression.c
+++ b/experiments/flat/src/Expression.c
@@ -40,10 +40,20 @@ Expression *ExpressionNew(ExprKind kind, int count, Expression **children)
     return expr;
 }
 
-Expression *ExpressionNewValue(ExprKind kind, const char *value)
+Expression *ExpressionNewIdent(const char *ident)
+{
+    Expression *expr = (Expression *)calloc(1,sizeof(Expression)+1*sizeof(Expression *));
+    expr->kind = IdentExpr;
+    expr->value = strdup(ident);
+    expr->count = 1;
+    expr->children[0] = NULL;
+    return expr;
+}
+
+Expression *ExpressionNewLit(const char *value)
 {
     Expression *expr = (Expression *)calloc(1,sizeof(Expression));
-    expr->kind = kind;
+    expr->kind = LitExpr;
     expr->value = strdup(value);
     expr->count = 0;
     return expr;
@@ -60,9 +70,16 @@ Expression *ExpressionNewRange(int lo, int hi)
 
 void ExpressionFree(Expression *expr)
 {
+    if (expr == NULL)
+        return;
     free(expr->value);
-    for (int i = 0; i < expr->count; i++)
-        ExpressionFree(expr->children[i]);
+    // Don't free children of IdentExpr, since these are expressions referenced by grammar
+    // rules, which will be freed separately. We can't use reference counting here as there
+    // will generally by cycles.
+    if (expr->kind != IdentExpr) {
+        for (int i = 0; i < expr->count; i++)
+            ExpressionFree(expr->children[i]);
+    }
     free(expr);
 }
 
@@ -264,3 +281,11 @@ void ExpressionPrint(Expression *expr, int highestPrecedence, const char
*indent
     if (brackets)
         printf(")");
 }
+
+void ExpressionSetTarget(Expression *expr, Expression *target)
+{
+    assert(expr->kind == IdentExpr);
+    assert(expr->count == 1);
+    assert(expr->children[0] == NULL);
+    expr->children[0] = target;
+}

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/9dcccd19/experiments/flat/src/Expression.h
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Expression.h b/experiments/flat/src/Expression.h
index a6b1306..348deb9 100644
--- a/experiments/flat/src/Expression.h
+++ b/experiments/flat/src/Expression.h
@@ -89,7 +89,8 @@ typedef enum {
 typedef struct Expression Expression;
 
 Expression *ExpressionNew(ExprKind kind, int count, Expression **children);
-Expression *ExpressionNewValue(ExprKind kind, const char *value);
+Expression *ExpressionNewIdent(const char *ident);
+Expression *ExpressionNewLit(const char *value);
 Expression *ExpressionNewRange(int lo, int hi);
 void ExpressionFree(Expression *expr);
 
@@ -100,3 +101,4 @@ const char *ExpressionValue(Expression *expr);
 int ExpressionStart(Expression *expr);
 int ExpressionEnd(Expression *expr);
 void ExpressionPrint(Expression *expr, int highestPrecedence, const char *indent);
+void ExpressionSetTarget(Expression *expr, Expression *target);

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/9dcccd19/experiments/flat/src/Grammar.c
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Grammar.c b/experiments/flat/src/Grammar.c
index 2187aed..74e36b4 100644
--- a/experiments/flat/src/Grammar.c
+++ b/experiments/flat/src/Grammar.c
@@ -73,6 +73,38 @@ void GrammarDefine(Grammar *gram, const char *name, Expression *expr)
     gram->nextDef = &def->next;
 }
 
+static Rule *GrammarLookup(Grammar *gram, const char *name)
+{
+    for (Rule *def = gram->defList; def != NULL; def = def->next) {
+        if (!strcmp(def->name,name))
+            return def;
+    }
+    return NULL;
+}
+
+static void GrammarResolveRecursive(Grammar *gram, Expression *expr, const char *ruleName)
+{
+    if (ExpressionKind(expr) == IdentExpr) {
+        const char *targetName = ExpressionValue(expr);
+        Rule *targetRule = GrammarLookup(gram,targetName);
+        if (targetRule == NULL) {
+            fprintf(stderr,"%s: Cannot resolve reference %s\n",ruleName,targetName);
+            exit(1);
+        }
+        ExpressionSetTarget(expr,targetRule->expr);
+    }
+    else {
+        for (int i = 0; i < ExpressionCount(expr); i++)
+            GrammarResolveRecursive(gram,ExpressionChild(expr,i),ruleName);
+    }
+}
+
+void GrammarResolve(Grammar *gram)
+{
+    for (Rule *def = gram->defList; def != NULL; def = def->next)
+        GrammarResolveRecursive(gram,def->expr,def->name);
+}
+
 void GrammarPrint(Grammar *gram)
 {
     int maxNameLen = 0;

http://git-wip-us.apache.org/repos/asf/incubator-corinthia/blob/9dcccd19/experiments/flat/src/Grammar.h
----------------------------------------------------------------------
diff --git a/experiments/flat/src/Grammar.h b/experiments/flat/src/Grammar.h
index d289ee6..ccbf012 100644
--- a/experiments/flat/src/Grammar.h
+++ b/experiments/flat/src/Grammar.h
@@ -25,4 +25,5 @@ typedef struct Grammar Grammar;
 Grammar *GrammarNew(void);
 void GrammarFree(Grammar *gram);
 void GrammarDefine(Grammar *gram, const char *name, Expression *expr);
+void GrammarResolve(Grammar *gram);
 void GrammarPrint(Grammar *gram);


Mime
View raw message