Want to improve this question? Add details and clarify the problem by editing this post.
开发者_开发知识库Closed 9 years ago.
Improve this questionCan someone explain to me how can I do to read some information from a file using regex?
The information that I want to read is in this format:
INT "integer"
FLOAT "real"
To be more precise, I hardly know how to do this using boost, so I need help.
It appears as though the language you wish to parse using regular expressions contains a finite number of keywords follows by identifiers consisting of a subset of symbols from your alphabet surrounded by quotation marks. Here is how to think about forming such a regular expression.
Let Σ be the set of terminal characters in your language. For each identifier, you must form a regular expression that looks like
- Rκ = κ · ' ' · " · (α ∪ β ∪ ... ∪ ψ ∪ ω)* · "
where κ is the desired keyword formed by concatenation of terminal symbols and α, β,..., ψ, ω ∈ Σ are the permissible identifier terminals. For instance, let Σ = {I,N,T,F,L,O,A,i,n,t,e,g,r,a,l,",,' '}. Then
- RINT = I · N · T · ' ' · " · (i ∪ n ∪ t ∪ e ∪ g ∪ r ∪ a ∪ l)* · "
- RREAL = R · E · A · L · ' ' · " · (i ∪ n ∪ t ∪ e ∪ g ∪ r ∪ a ∪ l)* · "
To form a regular expression that matches any sequence of the Rκ, merely form the Kleene closure of the union of the Rκ with whatever additions are necessary for the syntax you're working with, or the Kleene closure of the concatenation of the union of the subexpressions matching identifiers and the Kleene closure of the union of the permissible identifiers.
精彩评论