如何写WDI野牛语法? [英] How to write a bison grammer for WDI?

查看:195
本文介绍了如何写WDI野牛语法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在野牛语法结构有所帮助。

I need some help in bison grammar construction.

从我的另一个问题:
我试图做出一种元语言编写标记code(如XML和HTML)至极,可直接嵌入到C / C ++ code。
下面是用这种语言编写一个简单的示例,我把它称为WDI(Web开发接口):

From my another question: I'm trying to make a meta-language for writing markup code (such as xml and html) wich can be directly embedded into C/C++ code. Here is a simple sample written in this language, I call it WDI (Web Development Interface):

 /*
  * Simple wdi/html sample source code
  */
 #include <mySite>

 string name = "myName";
 string toCapital(string str);

 html
 {
  head {
   title { mySiteTitle; }
   link(rel="stylesheet", href="style.css");
  }
  body(id="default") {
   // Page content wrapper
   div(id="wrapper", class="some_class") {
    h1 { "Hello, " + toCapital(name) + "!"; }

    // Lists post
    ul(id="post_list") {
     for(post in posts) {
      li { a(href=post.getID()) { post.tilte; } }
     }
    }
   }
  }
 }

基本上它是一个C源代码HTML的用户友好的界面。
正如你可以看到传统的基于标签的样式由C-等取代的,用大括号分隔块。
我需要建立一个跨preTER翻译这个code到HTML和向后将其插入C,以便它可以被编译。在C部分,保持不变。
内的WDI源,没有必要使用印刷品,每return语句将用于输出(在printf函数)。
该程序的输出将是干净的HTML code。

Basically it is a C source with a user-friendly interface for html. As you can see the traditional tag-based style is substituted by C-like, with blocks delimited by curly braces. I need to build an interpreter to translate this code to html and posteriorly insert it into C, so that it can be compiled. The C part stays intact. Inside the wdi source it is not necessary to use prints, every return statement will be used for output (in printf function). The program's output will be clean html code.

因此​​,例如标题1标签将被转换这样的:

So, for example a heading 1 tag would be transformed like this:

h1 { "Hello, " + toCapital(name) + "!"; }
// would become:
printf("<h1>Hello, %s!</h1>", toCapital(name));

我的主要目标是建立一个跨preTER翻译WDI源为HTML这样的:

My main goal is to create an interpreter to translate wdi source to html like this:

标记(属性){内容} => &LT;标签属性&gt;内容&LT; /标签&GT;

其次,HTML code返回由各国preTER已被插入到C code。与printfs输出。内WDI发生变量和函数也应以使用它们作为printf的参数(转增资本(名称)的样品源的情况下)进行排序。

Secondly, html code returned by the interpreter has to be inserted into C code with printfs. Variables and functions that occur inside wdi should also be sorted in order to use them as printf parameters (the case of toCapital(name) in sample source).

下面是我的Flex /野牛文件:

Here are my flex/bison files:

id        [a-zA-Z_]([a-zA-Z0-9_])*
number    [0-9]+
string    \".*\"

%%

{id} {
        yylval.string = strdup(yytext);
        return(ID);
    }

{number} {
        yylval.number = atoi(yytext);
        return(NUMBER);
    }

{string} {
        yylval.string = strdup(yytext);
        return(STRING);
    }

"(" { return(LPAREN); }
")" { return(RPAREN); }
"{" { return(LBRACE); }
"}" { return(RBRACE); }
"=" { return(ASSIGN); }
"," { return(COMMA);  }
";" { return(SEMICOLON); }

\n|\r|\f { /* ignore EOL */ }
[ \t]+   { /* ignore whitespace */ }
.        { /* return(CCODE); Find C source */ }

%%


%start wdi
%token LPAREN RPAREN LBRACE RBRACE ASSIGN COMMA SEMICOLON CCODE QUOTE

%union
{
    int number;
    char *string;
}

%token <string> ID STRING
%token <number> NUMBER

%%
wdi
    : /* empty */
    | blocks
    ;

blocks
    : block
    | blocks block
    ;

block
    : head SEMICOLON
    | head body
    ;

head
    : ID
    | ID
    attributes
    ;

attributes
    : LPAREN RPAREN
    | LPAREN attribute_list RPAREN
    ;

attribute_list
    : attribute
    | attribute COMMA attribute_list
    ;

attribute
    : key ASSIGN value
    ;

key
    : ID {$$=$1}
    ;

value
    : STRING {$$=$1}
    /*| NUMBER*/
    /*| CCODE*/
    ;

body
    : LBRACE content RBRACE
    ;

content
    : /* */
    | blocks
    | STRING SEMICOLON
    | NUMBER SEMICOLON
    | CCODE
    ;

%%

我有上定义正确的语法的语言,特别是在分裂WDI和C code困难。我刚开始学习语言处理技术,所以我需要一些方向。
可能有人纠正我的code或给予什么是解决这一问题的正确方法的一些例子?

I am having difficulties on defining a proper grammar for the language, specially in splitting WDI and C code . I just started learning language processing techniques so I need some orientation. Could someone correct my code or give some examples of what is the right way to solve this problem?

推荐答案

如果你的意图是的解析<​​/ em>的C code和嵌入式WDI code,你是在硬骑。 LALR(1)语法分析器发电机(包括野牛)是出了名的坏解析C,更何况事情比C(即C + WDI)。

If your intention is to parse C code and embedded WDI code, you're in for a hard ride. LALR(1) parser generators (including Bison) are notoriously bad at parsing C, let alone things more complicated than C (meaning C + WDI).

要么你必须:

一)学习如何纠缠解析和符号表结构(意使野牛解析C,去斗争中与GNU GCC,看看他们是如何做到的),

a) learn how to make Bison parse C by tangling parsing and symbol table construction (meaning, go struggle with GNU GCC to see how they did it ),

B)切换到更强的解析器生成如GLR分析器生成器(其中野牛有一个选项),并学习如何处理模棱两可的语法和如何解决这些问题,

b) Switch to a stronger parser generator such as a GLR parser generator (which Bison has an option for) and learn how to deal with ambiguous grammars and how to resolve them,

c)设计WDI作为一种岛语法,其中的目标是挑选出WDI code,并留下未WDI不透明的字符串一切(你的情况注定要为$ P $输出psumed C code)。后一种方法更容易,并且大致就是所有的网页语言(ASP,PHP,JSP ...)做的。有利的一面是,这是很容易的,你只需要编写WDI本身的语法和词法分析器,将拿起一切不是WDI作为abitrary字符串。不利的一面是,你不会'能够使W​​DI和C很好互动/和/或解析器检查WDI程序的有效性。
见这太问题的一些背景:

c) design WDI as a kind of island grammer, in which the goal is to pick out the WDI code and leave everything that is not WDI as opaque strings (in your case destined to be output as presumed C code). This latter approach is much easier, and is roughly what all the web page languages (ASP, PHP, JSP ...) do. The upside is that this is much easier, and you only have to write the grammar for WDI itself and a lexer that will pick up everything that is not WDI as an abitrary string. The down side is that you wont' be able to make WDI and C interact nicely/and/or check the validity of a WDI program with your parser. See this SO question for some more background:

<一个href=\"http://stackoverflow.com/questions/2561249/island-grammar-antlr3\">http://stackoverflow.com/questions/2561249/island-grammar-antlr3

如果你去了解编译器技术更详细你开始这个项目之前,这会更容易些。

This would be easier if you go learn about compiler technology in more detail before you started this project.

这篇关于如何写WDI野牛语法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆