简体   繁体   中英

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). As I said before, I think I will have to use Regex but didn't find that very helpful so far.

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.

See Chomsky hierarchy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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