需要一个简单的 HTML Bison 语法

发布于 2024-10-13 18:29:26 字数 3238 浏览 3 评论 0原文

我查看了 Bison 帮助并写了这篇文章,但我不确定它是否完全正确。我还需要一个处理词法分析器的yylex()(它应该是Flex工具)。我了解上下文无关语法的一些基本知识。但我不知道如何正确实施它们! :(

我想要一个简单的 HTML Bison 语法。问题是:以下语法应该改变什么?

%{
    #include <stdio.h>
    int yylex(void);
    int yyerror(char const *);
%}

%token NUM_TOKEN FILENAME_TOKEN COLOR_TOKEN NAME_TOKEN

/* Html Grammer follows... */
%%


/* Any html tag follow this pattern: */
EXPRESSION: 
            '<' TAG CLUSER '>' INNER_EXPRESSION "</" TAG '>' ;

/* Some html tags: */
TAG: 
     "a"    |
     "html" |
     "head" |
     "link" |
     "div"  |
     "input"|
     "from" |
     "title"|
     "img"  |
     "table"|
     "td"   |
     "tr"   ;


CLUSER:
       ALIGN|
       CLASS|
       ID|
       SRC|
       TEPY|
       ACTION|
       HREF|
       REL|
       /* € (Eplsilone) */
       ;


ALIGN:
      "align" '=' "left"|
      "align" '=' "right"|
      "align" '=' "center"
      ;

CLASS:
      "class" '=' NAME_TOKEN
      ;

ID:
      "id" '=' NAME_TOKEN
      ;

SRC:
      "src" '=' FILENAME_TOKEN
      ;

TEPY:
      "type" '=' CONT
      ;

ACTION:
      "action" '=' FILENAME_TOKEN
      ;

HREF:
      "href" '=' '\"#\"'|
      "href" '=' FILENAME_TOKEN
      ;

REL:
      "rel" '=' "stylesheet"|
      "rel" '=' "\"stylesheet\""
      ;


DOMIN:
      "px"|
      "mm"|
      "cm"|
      "inch"
      ;

PAS:
     "php"|
     "asp"|
     "aspx"|
     "css"
     ;

CONT:
     "button"|
     "checkbox"|
     "text"|
     "password"|
     "file"|
     "submit"
     ;

INNER_EXPRESSION:
     EXPRESSION|
     /* € (Eplsilone) */
     ;


/* Html grammer ends. */
%% 

这是 Bison 的输出:

E:\Program Files\GnuWin32\bin>bison "E:\Dev-Cpp\HtmlBison\html.y" -o "E:\html.c"

E:\Dev-Cpp\HtmlBison\html.y: warning: 2 nonterminals useless in grammar
E:\Dev-Cpp\HtmlBison\html.y: warning: 8 rules useless in grammar
E:\\Dev-Cpp\\HtmlBison\\html.y:83.1-5: warning: nonterminal useless in grammar:
DOMIN
E:\\Dev-Cpp\\HtmlBison\\html.y:90.1-3: warning: nonterminal useless in grammar:
PAS
E:\\Dev-Cpp\\HtmlBison\\html.y:84.7-10: warning: rule useless in grammar: DOMIN:
 "px"
E:\\Dev-Cpp\\HtmlBison\\html.y:85.7-10: warning: rule useless in grammar: DOMIN:
 "mm"
E:\\Dev-Cpp\\HtmlBison\\html.y:86.7-10: warning: rule useless in grammar: DOMIN:
 "cm"
E:\\Dev-Cpp\\HtmlBison\\html.y:87.7-12: warning: rule useless in grammar: DOMIN:
 "inch"
E:\\Dev-Cpp\\HtmlBison\\html.y:91.6-10: warning: rule useless in grammar: PAS: "
php"
E:\\Dev-Cpp\\HtmlBison\\html.y:92.6-10: warning: rule useless in grammar: PAS: "
asp"
E:\\Dev-Cpp\\HtmlBison\\html.y:93.6-11: warning: rule useless in grammar: PAS: "
aspx"
E:\\Dev-Cpp\\HtmlBison\\html.y:94.6-10: warning: rule useless in grammar: PAS: "
css"
m4: cannot open `Files\GnuWin32/share/bison': No such file or directory
m4: cannot open `E:\Program': No such file or directory
m4: cannot open `Files\GnuWin32/share/bison/m4sugar/m4sugar.m4': No such file or
 directory

它不会是一个完整的 HTML 解析器。我只想验证非常简单的 HTML 文档,没有任何 CSS 样式或 JavaScript 或 ... 我还看到了这个。 注意:解决方案必须是 Bison 语法!

I've looked at the Bison help and have written this, but I'm not sure it's completely correct. Also i need an yylex() that handle Lexical Analyzer (It should be Flex tool). I know some basic things about context-free grammars. But i don't know how to implement them correctly! :(

I want a simple Bison grammar for HTML. The question is: What should change in following grammar?

%{
    #include <stdio.h>
    int yylex(void);
    int yyerror(char const *);
%}

%token NUM_TOKEN FILENAME_TOKEN COLOR_TOKEN NAME_TOKEN

/* Html Grammer follows... */
%%


/* Any html tag follow this pattern: */
EXPRESSION: 
            '<' TAG CLUSER '>' INNER_EXPRESSION "</" TAG '>' ;

/* Some html tags: */
TAG: 
     "a"    |
     "html" |
     "head" |
     "link" |
     "div"  |
     "input"|
     "from" |
     "title"|
     "img"  |
     "table"|
     "td"   |
     "tr"   ;


CLUSER:
       ALIGN|
       CLASS|
       ID|
       SRC|
       TEPY|
       ACTION|
       HREF|
       REL|
       /* € (Eplsilone) */
       ;


ALIGN:
      "align" '=' "left"|
      "align" '=' "right"|
      "align" '=' "center"
      ;

CLASS:
      "class" '=' NAME_TOKEN
      ;

ID:
      "id" '=' NAME_TOKEN
      ;

SRC:
      "src" '=' FILENAME_TOKEN
      ;

TEPY:
      "type" '=' CONT
      ;

ACTION:
      "action" '=' FILENAME_TOKEN
      ;

HREF:
      "href" '=' '\"#\"'|
      "href" '=' FILENAME_TOKEN
      ;

REL:
      "rel" '=' "stylesheet"|
      "rel" '=' "\"stylesheet\""
      ;


DOMIN:
      "px"|
      "mm"|
      "cm"|
      "inch"
      ;

PAS:
     "php"|
     "asp"|
     "aspx"|
     "css"
     ;

CONT:
     "button"|
     "checkbox"|
     "text"|
     "password"|
     "file"|
     "submit"
     ;

INNER_EXPRESSION:
     EXPRESSION|
     /* € (Eplsilone) */
     ;


/* Html grammer ends. */
%% 

This is Bison's output:

E:\Program Files\GnuWin32\bin>bison "E:\Dev-Cpp\HtmlBison\html.y" -o "E:\html.c"

E:\Dev-Cpp\HtmlBison\html.y: warning: 2 nonterminals useless in grammar
E:\Dev-Cpp\HtmlBison\html.y: warning: 8 rules useless in grammar
E:\\Dev-Cpp\\HtmlBison\\html.y:83.1-5: warning: nonterminal useless in grammar:
DOMIN
E:\\Dev-Cpp\\HtmlBison\\html.y:90.1-3: warning: nonterminal useless in grammar:
PAS
E:\\Dev-Cpp\\HtmlBison\\html.y:84.7-10: warning: rule useless in grammar: DOMIN:
 "px"
E:\\Dev-Cpp\\HtmlBison\\html.y:85.7-10: warning: rule useless in grammar: DOMIN:
 "mm"
E:\\Dev-Cpp\\HtmlBison\\html.y:86.7-10: warning: rule useless in grammar: DOMIN:
 "cm"
E:\\Dev-Cpp\\HtmlBison\\html.y:87.7-12: warning: rule useless in grammar: DOMIN:
 "inch"
E:\\Dev-Cpp\\HtmlBison\\html.y:91.6-10: warning: rule useless in grammar: PAS: "
php"
E:\\Dev-Cpp\\HtmlBison\\html.y:92.6-10: warning: rule useless in grammar: PAS: "
asp"
E:\\Dev-Cpp\\HtmlBison\\html.y:93.6-11: warning: rule useless in grammar: PAS: "
aspx"
E:\\Dev-Cpp\\HtmlBison\\html.y:94.6-10: warning: rule useless in grammar: PAS: "
css"
m4: cannot open `Files\GnuWin32/share/bison': No such file or directory
m4: cannot open `E:\Program': No such file or directory
m4: cannot open `Files\GnuWin32/share/bison/m4sugar/m4sugar.m4': No such file or
 directory

It's not going to be a complete HTML parser. I just want to validate very simple HTML documents without any CSS styles or JavaScripts or ...
I also saw this.
NOTE: The Solution must be a Bison grammar!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2024-10-20 18:29:26

TAG 应该是从词法分析器返回的标记,否则您将一直在写案例直到牛回家。

属性等也是如此。

TAG should be a token that is returned from a lexer, else you will be writing cases till the cows come home.

Same goes for attributes, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文