使用Flex& amp;的简单Java语法野牛 [英] Simple Java grammar using Flex & Bison

查看:89
本文介绍了使用Flex& amp;的简单Java语法野牛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始学习基本的Flex和Bison,因为我必须为简单(但不太简单)的语法创建解析器.我决定在语法上简化Java语言.我制作了.l.y文件,所有内容编译都没有错误(我正在使用gcc进行编译). 问题是,每次运行生成的程序时,即使使用简单输入private class Something{},我也会得到Syntax Error.我唯一没有得到Syntax Error的地方是当我输入一个空行(\n)时. 我已经为此苦苦挣扎了几天,我怀疑我的语法中存在问题,但是我似乎找不到.当然,可能还存在其他问题,因为我是Flex和Bison的新手.

I recently started learning basic Flex and Bison because I have to make a parser for simple (but not too simple) grammar. I decided to make a simplified Java language in my grammar. I made the .l and the .y files and everything compiles without error (I'm using gcc to compile). The problem is that every time I run the generated program I get Syntax Error, even with a simple input like: private class Something{}. The only time I do not get a Syntax Error is when I enter an empty line (\n). I've been struggling with this for a few days now and I suspect there's somewhere a problem in my grammar but I can't seem to find it. Of course there may be other problems too because I'm pretty new to Flex and Bison.

任何帮助将不胜感激.

Any help would be really appreciated.

这是.l.y文件:

java.l

%{
#include "java.tab.h"
%}

%option noyywrap

%%

"\n" return 0;
[ \t] ;

"private" {return PRIVATE;}
"public" {return PUBLIC;}
"protected" {return PROTECTED;}
"implenets" {return IMPLEMENTS;}
"extends" {return EXTENDS;}
"class" {return CLASS;}
"interface" {return INTERFACE;}
"if" {return IF;}
"while" {return WHILE;}
"return" {return RETURN;}
"true" {return BOOLEAN;}
"false" {return BOOLEAN;}

[A-z][a-z0-9]* {return NAME;}

"\""[A-z0-9]*"\"" {return STRING;}
"-"?[1-9][0-9]* {return INT;}

"+"|"-"|"*"|"/"|"="|"==" {return OPERATOR;}

%%

java.y

%{
#include <stdio.h>

int cond=0;
int loops=0;
int assigns=0;
int funcs=0;
int classes=0;

void yyerror(const char* msg){printf("Error: %s\n", msg);}
%}


%token PUBLIC
%token PRIVATE
%token PROTECTED
%token NAME
%token IMPLEMENTS
%token EXTENDS
%token CLASS
%token INTERFACE
%token IF
%token WHILE
%token STRING
%token BOOLEAN
%token OPERATOR
%token RETURN 
%token INT

%%

Code: Class Code | /*empty*/ {printf("classes: %d\n", classes); printf("functions: %d\n", funcs); printf("conditions: %d\n", cond); 
                                printf("loops: %d\n", loops); printf("assign operations: %d\n", assigns);} ;
Class: Modifier ClassType NAME Extra '{' Functions '}' ;
Modifier: PUBLIC | PRIVATE | PROTECTED ;
ClassType: CLASS | INTERFACE ;
Extra: IMPLEMENTS NAME | EXTENDS NAME | /*empty*/ ;
Functions: Function Functions | /*empty*/ ;
Function: Type NAME '(' Arguments ')' '{' Commands '}' {funcs++;} ;
Arguments: Argument Arguments | /*empty*/ ;
Argument: Type NAME Separator ;
Type: STRING | INT | BOOLEAN ;
Separator: ',' | /*empty*/ ;
Commands: Command Commands | /*empty*/ ;
Command: Condition | Loop | Assignment | Return ;
Condition: IF '(' Comparison ')' '{' Commands '}' {cond++;} ;
Loop: WHILE '(' Comparison ')' '{' Commands '}' {loops++;} ;
Comparison: NAME OPERATOR INT | NAME OPERATOR NAME | INT OPERATOR NAME ;
Assignment: NAME '=' Type ';' {assigns++;} ;
Return: RETURN RetVal ';' ;
RetVal: NAME | Type ;

%%

int main()
{
   yyparse();
   return 0;
}

推荐答案

这是一个开始:

首先,flex提供的默认规则只是回显与其他任何规则都不匹配的字符. {}不受任何规则匹配,因此它们将被回响,并且不会被野牛看到,这使得产品Class无法匹配.一个简单的解决方案是将默认规则作为最后一个flex规则:

First, the default rule provided by flex just echoes the character unmatched by any other rule. { and } are not matched by any rule, so they will be echoed and never be seen by bison, which makes it impossible for the production Class to match. A simple solution is to put a default rule as the last flex rule:

. { return yytext[0]; }

第二,[A-z][A-Za-z]不同,因为Za在ASCII中不是连续的.我建议对字母字符使用[[:alpha:]],对字母数字使用[[:alnum:]],但是[A-Za-z][A-Za-z0-9]并没有错.在这两种情况下,您可能都希望允许使用其他字符,例如_. (这不会给您带来任何直接的问题,这只是一个说明.)

Second, [A-z] is not the same as [A-Za-z] because Z and a are not consecutive in ASCII. I recommend using [[:alpha:]] for alphabetic characters and [[:alnum:]] for alphanumerics, but there's nothing wrong with [A-Za-z] and [A-Za-z0-9]. In both cases, you might want to allow other characters, such as _. (That's not causing you any immediate problems, it's just a note.)

第三,您拼错了"implements".

这篇关于使用Flex&amp; amp;的简单Java语法野牛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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