简体   繁体   English

Java中的正则表达式(正则表达式和语法)

[英]Regex in Java (Regular Expression and Grammar)

That is my problem: I got an assignment due tomorrow (I have been working on it for the past days) and I think a got half of it, but the other half I just got a slight clue. 那就是我的问题:我有一个明天要交的作业(过去几天我一直在努力),我想其中的一半,但是另一半我只是有一点头绪。 Basically, I have to write a code where the user gives me some words and their categories (like a dictionary) and a grammar eg: 基本上,我必须编写一个代码,其中用户给我一些单词及其类别(例如字典)和语法,例如:

A: article adjective A  
A: adjective B  
B: noun  

then the user will input a sentence and I will check if the words are in my dictionary, if so I will output their category and "Accept" if it matches the grammar and "Refuse" if it doesn't match. 然后用户将输入一个句子,然后我将检查单词是否在字典中,如果是,我将输出其类别,如果与语法匹配,则输出“接受”,如果与语法不匹配,则输出“拒绝”。

Now, I know that I probably will have to use regex but I didn't find anything that really helped so far. 现在,我知道我可能必须使用正则表达式,但到目前为止,我还没有发现真正有用的东西。
I will give a example of input and output: 我将给出输入和输出的示例:

Input:  
Dictionary:  
word: The  
category: article  
word: big  
category: adjective  
word: blue  
category: adjective  
word: car   
category: noun

Grammar:  
A: article adjective A  
A: adjective B  
B: noun  

Sentences:   
The big blue car  
The car big blue  

Output:  
(first sentence)  
article adjective adjective noun  
Accept  
(second sentence)  
article noun adjective adjective   
Refuse  

So, my program is already outputting the correct categories but I don't know how to implement the grammar part (I already have a class with a arrayList for the grammar). 因此,我的程序已经输出了正确的类别,但是我不知道如何实现语法部分(我已经有了一个带有arrayList的类)。 As I said before, I think I will have to use Regex but didn't find that very helpful so far. 正如我之前说过的,我认为我必须使用Regex,但到目前为止还没有发现太大的帮助。

Thank you in advance! 先感谢您!

Grammars are not usually implemented as regex since they are complex and self referencing, regex are more useful for parsing the incoming text. 语法通常不作为正则表达式实现,因为它们很复杂且具有自引用功能,因此正则表达式对于解析传入的文本更为有用。 What you need is a better data structure than a list to represent the grammars and help you verify if a sentence is grammatically correct. 您需要的是比列表更好的数据结构来表示语法,并帮助您验证句子在语法上是否正确。 Look into abstract syntax trees. 查看抽象语法树。

You can't generally implement a grammar with a regular expression, unless it is a grammar for a regular language, for example doesn't contain nested syntax such as 通常,您不能使用正则表达式实现语法,除非它是常规语言的语法,例如不包含嵌套语法,例如

 expression: '(' expression ')'

You need a pushdown automaton of some kind, eg a recursive descent or LALR(1) parser. 您需要某种下推式自动机,例如递归下降或LALR(1)解析器。

See Chomsky hierarchy . 请参阅Chomsky层次结构

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM